I currently use this script to obtain the SID of a user from AD. Not that each time I need an SID, I have to open the script and type the persons username in, which when I have 100's to do can be frustrating. The current script is as follows:
$name = "username"
(New-Object System.Security.Principal.NTAccount($name)).Translate([System.Security.Principal.SecurityIdentifier]).Value
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Is there a way that I can use the same script but put the AD usernames in a textfile and pull them into powershell and have the output come out like so I get the username and the SID. Ideally into CSV format?
Cheers,
Assuming you have a list of usernames, each on its own row in userlist.txt, the process is not too complicated.
# Array for name&sid tuples
$users = #()
cat C:\temp\userlist.txt | % {
# Syntax sugar
$spSid = [Security.Principal.SecurityIdentifier]
# Custom object for name, sid tuple
$user = new-object psobject
$user | Add-Member -MemberType noteproperty -name Name -value $null
$user | Add-Member -MemberType noteproperty -name Sid -value $null
$user.Name = $_
$user.Sid = (New-Object Security.Principal.NTAccount($_)).Translate($spSid).Value
# Add user data tuple to array
$users += $user
}
# Export array contents into a CSV file
$users | Select-Object -Property Name,Sid | Export-Csv -NoTypeInformation C:\temp\sidlist.txt
Related
I am trying to get a powershell script to export all users in an OU and sub OUs which I can do fine, but when I try to get the user's OU, I get nothing for the OU. I have looked all over online and found a few scripts that pull just the user's OU, but they are a little slow and I can't seem to get them to pull groups or is for pulling from one group instead of listing all users and their groups.
I am trying to export this list and sort by OU so that I can ensure each student is in the proper groups. We have had a few students that were in extra groups and I want a quick and easy look to find those students.
#Student
$Report = #()
#Collect all users
$Users = Get-ADUser -Filter * -SearchBase 'OU=Student,DC=domain,DC=com' -Properties distinguishedname, Name, GivenName, SurName, SamAccountName, UserPrincipalName, MemberOf, Enabled -ResultSetSize $Null
# Use ForEach loop, as we need group membership for every account that is collected.
# MemberOf property of User object has the list of groups and is available in DN format.
Foreach($User in $users){
$UserGroupCollection = $User.MemberOf
#This Array will hold Group Names to which the user belongs.
$UserGroupMembership = #()
#To get the Group Names from DN format we will again use Foreach loop to query every DN and retrieve the Name property of Group.
Foreach($UserGroup in $UserGroupCollection){
$GroupDetails = Get-ADGroup -Identity $UserGroup
#Here we will add each group Name to UserGroupMembership array
$UserGroupMembership += $GroupDetails.Name
}
#As the UserGroupMembership is array we need to join element with ',' as the seperator
$Groups = $UserGroupMembership -join ','
#Creating custom objects
$Out = New-Object PSObject
$Out | Add-Member -MemberType noteproperty -Name DistinguishedName -Value #{Name="DistinguishedName";Expression={$_.distinguishedname | ForEach-Object {$_ -replace '^.+?(?<!\\),',''}}}
$Out | Add-Member -MemberType noteproperty -Name Name -Value $User.Name
$Out | Add-Member -MemberType noteproperty -Name UserName -Value $User.SamAccountName
$Out | Add-Member -MemberType noteproperty -Name Status -Value $User.Enabled
$Out | Add-Member -MemberType noteproperty -Name Groups -Value $Groups
$Report += $Out
}
#Output to screen as well as csv file.
$Report | Sort-Object DistinguishedName | FT -AutoSize
$Report | Sort-Object DistinguishedName | Export-Csv -Path $env:temp\students.csv -NoTypeInformation
There you go, I added some comments to help you understand the thought process.
This should be a lot faster than what you were doing.
The problem while adding your OUs was here:
$Out | Add-Member -MemberType noteproperty -Name DistinguishedName -Value #{Name="DistinguishedName";Expression={$_.distinguishedname | ForEach-Object {$_ -replace '^.+?(?<!\\),',''}}}
Which should've been:
$Out | Add-Member -MemberType noteproperty -Name DistinguishedName -Value ($user.distinguishedname -replace '^.+?(?<!\\),','')
#Student
$Report = [system.collections.generic.list[pscustomobject]]::new()
# Using Collection.Generic.List instead of System.Array for efficiency
#Collect all users
$Users = Get-ADUser -Filter * -SearchBase 'OU=Student,DC=domain,DC=com' -Properties MemberOf
# -> Use -SearchScope Subtree if you want to go all the way down in OU recursion starting from the 'OU=Student'
# -> distinguishedname, Name, GivenName, SurName, SamAccountName, UserPrincipalName and Enabled are Default Properties
# of Get-ADUser, no need to call them.
# -> -ResultSetSize $Null is default for Get-ADUSer, no need to call it
# Use ForEach loop, as we need group membership for every account that is collected.
# MemberOf property of User object has the list of groups and is available in DN format.
Foreach($User in $users)
{
#This Array will hold Group Names to which the user belongs.
$UserGroupMembership = [system.collections.generic.list[string]]::new()
#To get the Group Names from DN format we will again use Foreach loop to query every DN and retrieve the Name property of Group.
Foreach($UserGroup in $User.MemberOf)
{
# $GroupDetails = Get-ADGroup -Identity $UserGroup
# -> Instead of this, we can do some string manipulation
# which will be a lot faster and give you the same results.
$UserGroupMembership.Add($UserGroup.Split(',OU=')[0].replace('CN=',''))
}
#As the UserGroupMembership is array we need to join element with ',' as the seperator
$Groups = $UserGroupMembership -join ','
#Creating custom objects
<#
$Out = New-Object PSObject
$Out | Add-Member -MemberType noteproperty -Name DistinguishedName -Value #{Name="DistinguishedName";Expression={$_.distinguishedname | ForEach-Object {$_ -replace '^.+?(?<!\\),',''}}}
$Out | Add-Member -MemberType noteproperty -Name Name -Value $User.Name
$Out | Add-Member -MemberType noteproperty -Name UserName -Value $User.SamAccountName
$Out | Add-Member -MemberType noteproperty -Name Status -Value $User.Enabled
$Out | Add-Member -MemberType noteproperty -Name Groups -Value $Groups
$Report += $Out
-> Again, Add-Member is highly inefficient compared to casting PSCustomObject
-> += is evil ( •̀ᴗ•́ )و ̑̑
#>
$Report.Add(
[pscustomobject]#{
OrganizationalUnit = ($user.DistinguishedName -replace '^.+?(?<!\\),','')
Name = $user.Name
UserName = $user.sAMAccountName
Status = $user.Enabled
Membership = $Groups
})
}
#Output to screen as well as csv file.
$Report | Sort-Object OrganizationalUnit | FT -AutoSize
$Report | Sort-Object OrganizationalUnit | Export-Csv -Path $env:temp\students.csv -NoTypeInformation
I don't know how many users you have but every time you += on an array the entire array plus the new element is copied to a completely new array. This is a bad practice and gets exponentially worse with every item added the array. You can avoid this by building the arrays as a loop result or by using dotnet list object with an efficient add() method.
You also look up the same group names repeatedly. I don't know the numbers but it's probably a lot better to put all your groups in a hashtable once and then look them up.
Your question is unclear, but if you want a list of users and their groups, you are going the long way around. You mention the ou but AFAICS there is no org unit used in the code. Do you want the AD ou property or a part of the DN? You don't seem to be using either.
Note that the DN is a string and sorting by DN will just give an alpha string sort which is not helpful. Are your students in separate org units under OU=students ? This is not clear. If so, use the AD canonicalName to sort the list.
No need to include default properties in -property. Splatting is nice.
You should improve your question by indicating what your AD structure looks like and what you think your output should look like.
Also, format your code for readability.
You want something along these lines:
# group hashtable, for efficient name lookup
$groupName = #{}
$ignoredGroups = #( 'AllStudents','AllUsers', 'etc' ) # don't clutter list with these groups
Get-AdGroup -filter '*' | # any restrictions? searchbase, etc
ForEach-Object {
if ( $ignoredGroups -notcontains $_.Name ) {
$groupName[ $_.distinguishedName ] = $_.Name
}
}
# ADsplat, for readability
$AD_Splat = #{
Filter = '*'
SearchBase = 'OU=Student,DC=domain,DC=com'
Properties = 'MemberOf,CanonicalName,sn,givenName'.split(',') # split to array
ResultSetSize = $Null # !? also, there are system limits to size
}
$results = Get-ADUser #ad_splat |
ForEach-Object {
$DN = $_.distinguishedName # do you need this at all?
$CName = $_.canonicalName # for sorting by AD org unit
$XName = $_.sn + ', ' + $_.givenName
if ( $_.Enabled ) { $Enabled = 'Y'} else { $Enabled = '.' }
$groups = (
$_.memberOf |
ForEach-Object { $GroupName[ $_ ] } | # lookup name
where-Object { $_ } | # ignore nulls (when group not in hashtable)
sort-object # consistent ordering between users
) -join ';' # don't use comma, csv conflict
# leave custom object in pipe! This builds the array efficiently.
New-Object PSObject -Property #{
DistinguishedName = $dn
Name = $_.name
XName = $XName
Login = $_.SamAccountName
CName = $CName
Groups = $Groups
}
} | Sort-Object CName # sort the objects by canonical name
$results | format-table
$results | Export-Csv -Path 'c:\temp\usersgroups.csv' -NoTypeInformation
I have a script to get a list of all installed software using the registry keys. But when I check the list, some entries don't have a name or version shown. Below is my script:
## Include CSV file of all computers with header "pc"
$computers = Import-Csv "C:\Users\P1334126\Documents\Test.CSV"
$array = #()
#Define the variable to hold the location of Currently Installed Programs
foreach($pc in $computers) {
$computername=$pc.computername
$UninstallKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
#Create an instance of the Registry Object and open the HKLM base key
$reg = [microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine', $computername)
#Drill down into the Uninstall key using the OpenSubKey Method
$regkey = $reg.OpenSubKey($UninstallKey)
#Retrieve an array of string that contain all the subkey names
$subkeys = $regkey.GetSubKeyNames()
#Open each Subkey and use GetValue Method to return the required values for each
foreach($key in $subkeys) {
$thisKey=$UninstallKey + '\\' + $key
$thisSubKey=$reg.OpenSubKey($thisKey)
$obj = New-Object PSObject
$obj | Add-Member -MemberType NoteProperty -Name 'ComputerName' -Value $computername
$obj | Add-Member -MemberType NoteProperty -Name 'DisplayName' -Value $($thisSubKey.GetValue("DisplayName"))
$obj | Add-Member -MemberType NoteProperty -Name 'DisplayVersion' -Value $($thisSubKey.GetValue("DisplayVersion"))
$obj | Add-Member -MemberType NoteProperty -Name 'Publisher' -Value $($thisSubKey.GetValue("Publisher"))
$array += $obj
}
}
$array | Select-Object ComputerName, DisplayName, DisplayVersion, Publisher | Sort-Object -Property
ComputerName | Out-File InstalledSoftware.txt
As shown in the picture below, some names and versions are misssing, is there a reason for this? Appreciate the help.
You really should avoid collecting stuff in an array using += syntax as it completely recreates the array on every iteration. It makes the code slow and is memory consuming.
Also, I would advise to Close the registry keys you have opened after you're done with them.
As for output, you are currently saving the results to a simple text file, but I think (since you have gathered nice objects) outputting to CSV file would be better as this preserves the info as readable data you can open in Excel for instance.
Your code revised:
# Include CSV file of all computers with header "pc"
$computers = Import-Csv "C:\Users\P1334126\Documents\Test.CSV"
# probe these two registry paths
$UninstallPaths = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',
'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
# Collect info of Currently Installed Programs
$array = foreach ($pc in $computers) {
$computername = $pc.computername
# Create an instance of the Registry Object and open the HKLM base key
$regHKLM = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $computername)
foreach ($UninstallKey in $UninstallPaths) {
# Drill down into the Uninstall key using the OpenSubKey Method
$regkey = $regHKLM.OpenSubKey($UninstallKey)
# the computer may not have the 'Wow6432Node' registry key
if (!$regkey) { break }
# Retrieve an array of string that contain all the subkey names
$subkeys = $regkey.GetSubKeyNames()
# Open each Subkey and use GetValue Method to return the required values for each
foreach($key in ($regkey.GetSubKeyNames())) {
$thisPath = Join-Path -Path $UninstallKey -ChildPath $key
$thisSubKey = $regHKLM.OpenSubKey($thisPath)
# we want to output only if there is at least a DisplayName to show
$displayName = $thisSubKey.GetValue("DisplayName")
if (![string]::IsNullOrWhiteSpace($displayName)) {
# output a PSObject to be collected in variable $array
# the order of the properties also defines the order of the fields in the output
[PsCustomObject] #{
ComputerName = $computername
DisplayName = $displayName
DisplayVersion = $thisSubKey.GetValue("DisplayVersion")
Publisher = $thisSubKey.GetValue("Publisher")
# if you want to know the registry path aswell, uncomment the next entry
# RegistryPath = $thisPath
}
}
# close the subkey
$thisSubKey.Close()
}
# close the $regKey
$regkey.Close()
}
# close the base key
$regHKLM.Close()
}
# sort on ComputerName
$array = $array | Sort-Object -Property ComputerName
# output on screen
$array | Format-Table -AutoSize
# or if you prefer
$array | Out-GridView -Title 'InstalledSoftware'
# output to CSV file you can open in Excel
$array | Export-Csv -Path 'InstalledSoftware.csv' -NoTypeInformation
Instead of the registry you can use this to list installed software in powershell 5.1:
get-package
I'm having some trouble copying data from 1 CSV and pasting it into a template of another one.
The template has specific column names.
and the csv file I have with the data, I'm able to get each column, but I am having trouble pasting it into a the template.
I'm trying to copy the following data from 1 csv to the template and here are the columns
email --> internal_customer_ID
givenname --> first_name
surname --> last_name
mail --> email_address
mobilephone --> mobile_phone_1
officePhone --> landline_phone_1
Here is my current code.
#Clear Screen
CLS
#The path we are working with (use the path where we execute this script from)
$global:path = Split-Path $script:MyInvocation.MyCommand.Path
$DataFile = $path + "\dataFile.csv"
$ExportedFileCSV = $path + "\PopulatedTemplate.csv"
#Export the data file with our user info
Get-ADGroupMember -Identity SomeGroup | Get-ADUser -Properties * | Select-Object -Property GivenName, SurName, Mail, MobilePhone, OfficePhone | Export-Csv -path $DataFile -NoTypeInformation -Force
$dataInput = Import-Csv $DataFile
$dataOutput = Import-Csv $ExportedFileCSV
$dataInput | ForEach-Object {
$newData = $_
$dataOutput |
Add-Member -MemberType NoteProperty -Name "internal_customer_ID" -Value $newData.Mail -PassThru -Force|
Add-Member -MemberType NoteProperty -Name "landline_phone_1" -Value $newData.OfficePhone -PassThru -Force|
Add-Member -MemberType NoteProperty -Name "email_address" -Value $newData.Mail -PassThru -Force|
Add-Member -MemberType NoteProperty -Name "mobile_phone_1" -Value $newData.MobilePhone -PassThru -Force|
Add-Member -MemberType NoteProperty -Name "last_name" -Value $newData.SurName -PassThru -Force|
Add-Member -MemberType NoteProperty -Name "first_name" -Value $newData.GivenName -PassThru -force
} | Export-CSV $ExportedFileCSV
If I can avoid exporting the datafile in the first place and just appending the result from the
Get-ADGroupMember -Identity SomeGroup | Get-ADUser -Properties * | Select-Object -Property GivenName, SurName, Mail, MobilePhone, OfficePhone
Straight to the csv template, that would work for my needs too, I just wasn't sure how to do that.
Your line reading $dataOutput | Add-Member ... is the problem, I think. Add-Member is for adding an attribute to a single object, but $dataOutput at this point is a collection of objects. I think the interpreter thinks you're trying add a member attribute to an object array.
Try creating a new object for each output record, then do an Export-CSV -append onto your output CSV file.
I think something like this should work:
$dataInput | ForEach-Object {
$newData = $_
$newRecordProperties = [ordered]#{
"internal_customer_ID"=$newData.Mail
"landline_phone_1" = $newData.OfficePhone
"email_address" = $newData.Mail
"mobile_phone_1" = $newData.MobilePhone
"last_name" = $newData.SurName
"first_name" = $newData.GivenName
}
$newRecord = new-object psobject -Property $newRecordProperties
Write-Output $newRecord
} | Export-CSV $ExportedFileCSV -Append
As long as the columns names in the output CSV are the same as your new record object, I think it should be okay. I am not sure what happens if the columns in $ExportedFileCSV are in a different order than the $newRecord being exported, so I added [ordered] to the hash table. You may want to test this yourself.
For the second part of your question, pipe-lining the whole thing, something like this is probably what you're after:
Get-ADGroupMember -Identity SomeGroup |
Get-ADUser -Properties * |
Select-Object -Property #(
#{label="internal_customer_ID"; expression={$_.Mail}}
#{label="email_address"; expression={$_.Mail}}
#{label="landline_phone_1"; expression={$_.OfficePhone}}
#{label="first_name"; expression={$_.GivenName}}
#{label="last_name"; expression={$_.SurName}}
#{label="mobile_phone_1"; expression={$_.MobilePhone}}
) |
Export-Csv $ExportedFileCSV -Append
Select-Object above creates a custom object with the attribute name and attribute value matching label and the result of expression. Again, re-order to match the order the CSV columns should be in.
Good Afternoon,
I searched through this forum and a few others combining ideas and trying different angles but haven't figured this out. If this has been answered, and I suck at searching, I am sorry and please let me know.
End Goal of script: have an excel file with columns for the AD properties Name, Office, Org, and most importantly a seperate column for each group a user is a member of.
The problem I am running into is creating a new column for each/every group that a user has. Not all users have the same amount of groups. Some have 10 some have 30 (yes 30, our AD is a mess).
Here is what I have done so far, and the spot that I am having difficulty with is towards the end:
$scoop = get-content C:\temp\SCOOP.txt ##This is a text file with a list of user id's, to search AD with
$outfile = 'C:\temp\SCOOP_ID.csv'
$ou = "OU=Humans,OU=Coupeville,DC=ISLANDS" #This is the searchbase, helps AD isolate the objects
Clear-Content $outfile #I clear content each time when I am testing
Foreach($ID in $scoop){
##AD Search filters##
$filtertype = 'SamAccountName'
$filter1 = $ID
##End AD Search filters##
##AD Search --MY MAIN ISSUE is getting the MemberOF property properly
$properties = get-aduser -SearchBase $ou -Filter {$filtertype -eq $filter1} -Properties Name,Office,Organization,MemberOf | select Name,Office,Organization,MemberOf
##AD Search ## Turns the MemberOf property to a string, I tried this during my testing not sure if objects or strings are easier to work with
#$properties = get-aduser -SearchBase $ou -Filter {$filtertype -eq $filter1} -Properties Name,Office,Organization,MemberOf | select Name,Office,Organization, #{n='MemberOf'; e= { $_.memberof | Out-String}}
#Attempt to specify each property for the output to csv
$name = $properties.Name
$office = $properties.Office
$org = $properties.Organization
$MemberOf = $properties.MemberOf
$membersArray = #()
foreach($mem in $MemberOf){ $membersArray += $mem }
###This is what I typically use to export to a CSV - I am sure there are other maybe better ways but this one I know.
$Focus = [PSCustomObject]#{}
$Focus | Add-Member -MemberType NoteProperty -Name 'Name' -Value $name -Force
$Focus | Add-Member -MemberType NoteProperty -Name 'Office' -Value $office -Force
$Focus | Add-Member -MemberType NoteProperty -Name 'Org' -Value $org -Force
$Focus | Add-Member -MemberType NoteProperty -Name 'Groups' -Value $MemberOf -Force
<########################
#
# Main ISSUE is getting the $memberOf variable, which contains all of the users groups, into seperate columns in the csv. To make it more plain, each column would be labeled 'Groups', then 'Groups1', and so on.
I have tried a few things but not sure if any were done properly or what I am messing up. I tried using $memberof.item($i) with a FOR loop but couldnt figure out how to send each
item out into its own Add-Member property.
#
#######################>
##Final Output of the $Focus Object
$Focus | Export-Csv $outfile -Append -NoTypeInformation
}
I came up with this solution, simply loop $MemberOf and add a unique name.
$MemberOf = #('a','b','c','d','e','f') #example
$Focus = [PSCustomObject]#{}
$Focus | Add-Member -MemberType NoteProperty -Name 'Name' -Value 'test' -Force
$i=0; $MemberOf | % {
$i++
$Focus | Add-Member -MemberType NoteProperty -Name "Group $i" -Value $_ -Force
}
$Focus | Export-Csv xmgtest1.csv -Append -Force -NoTypeInformation
However this doesn't work in your case, since you run this code once per user and there's no "awareness" of the entire set. So you would (in your existing architecture) need to ensure that the largest number of groups is added first.
You can fix this by creating the CSV all at once (only other option would be to constantly load/unload the csv file).
First, add $csv = #() to the top of your file.
Then, after you finish creating $Focus, add it to $csv.
Finally, you may remove -Append.
The slimmed down version looks like this:
$csv = #()
#Foreach($ID in $scoop){ etc..
$MemberOf = #('a','b','c','d','e','f') #example
$Focus = [PSCustomObject]#{}
$Focus | Add-Member -MemberType NoteProperty -Name 'Name' -Value 'test' -Force
#$Focus | Add-Member etc ...
$i=0; $MemberOf | % {
$i++
$Focus | Add-Member -MemberType NoteProperty -Name "Group $i" -Value $_ -Force
}
$csv += $Focus
# } end of Foreach($ID in $scoop)
$csv | Export-Csv xmgtest1.csv -NoTypeInformation
Hope this helps.
I have a small script which retrieves the LastLogonTimestamp and the SAMAccount for all users in a particular OU in AD and converts the timestamp to a date and extracts just the date from the string. That part works fine. I then would like to output that to a CSV so it may be opened in Excel and be perfectly formated into columns and look all pretty.
I have tried ConvertTo-Csv and Export-Csv but have been uncuccessful. The problem is I am new to Powershell. This is my first script and I don't fully understand how this works. My script is probably terribly messy and illogical but it does the job so far.
Please help. Thanks.
$userlist = Get-ADUser -SearchBase "OU=IT,DC=whatever,DC=com,DC=au" -Filter * -Properties * | Select-Object -Property Name,LastLogonTimestamp,SAMAccountName | Sort-Object -Property Name
$userlist | ForEach-Object {
$last = $_.LastLogonTimestamp;
$ADName = $_.SAMAccountName;
$tstamp = w32tm /ntte $last;
if($tstamp.Length -lt "40"){}else
{
$ADDate = [DateTime]::Parse($tstamp.Split('-')[1]).ToString("dd/MM/yyyy")
write-host $ADDate;
write-host $ADName;
}
}
You will have to create objects for each user and pipe those to the Export-CSV cmdlet:
$usersList | %{
# current logic
$user = new-object psobject
$user | add-member -membertype noteproperty -name LastLogon -value $last
$user | add-member -membertype noteproperty -name ADName -value $ADName
$user | add-member -membertype noteproperty -name ADDate -value $ADDate
$user
} | export-csv test.csv -notype
Alternative syntax for populating the object:
$properties = #{"LastLogon" = $last; "ADName" = $ADName; "ADDate" = $ADDate}
$user = new-object psobject -property $properties