I have now been creating a scrip that pulls all users from Active Directory, lists them up in alphabetical order and list up the groups each user belongs to.<\/p>\n
Now I have some general programming knowledge but this is the first time I’m making a PowerShell scripts so I have been scouring the interwebs and the code below is what I have come up with so far.<\/p>\n
#// Start of script\n#// Original script found at https://community.spiceworks.com/\n#// topic/381837-script-to-list-all-active-users-and-their-\n#// group-membership-in-a-domain\n#// Modified by arnfridur\n\n#// Get year and month for csv export file\n$DateTime = Get-Date -f \"yyyy-MM\"\n\n#// Set CSV file Name\n$CSVFile = \"C:\\ADUsers&Groups\"+$DateTime+\".csv\"\n\n#// Create empty array for CSV data\n$CSVOutput = @()\n\n#// Get all AD users in the domain\n$ADUsers = Get-ADUser -Filter *\n\n#// Set progress bar variables\n$i=0\n$tot = $ADUsers.count\n\nforeach ($User in $ADUsers) {\n #// Set up progress bar\n $i++\n $status = \"{0:N0}\" -f ($i / $tot * 100)\n Write-Progress -Activity \"Exporting AD Users\" -status \"Processing User $i of $tot : $status% Compleated\" -PercentComplete ($i / $tot * 100)\n\n #// Ensure Groups variable is empty\n $Groups = \"\"\n\n #// Get groups and add to string\n $GroupArr = Get-ADUser -filter {Name -eq $ADUser.Name} | Get-ADPrincipalGroupMembership | select Name\n\n if ($GroupArr) {\n foreach ($Group in $GroupArr) {\n $Groups = $Groups + \",\" + $Group.Name\n }\n $Groups = $Groups.Substring(1,($Groups.Length) -1)\n }\n\n #// Set up hash table and add values\n $HashTab = $null\n $HashTab = [ordered]@{\n \"Name\" = $ADUsers.Name\n \"Description\" = $ADUsers.DisplayName\n \"Groups\" = $Groups\n }\n\n #// Add hash table to CSV data array\n $CSVOutput += New-Object PSObject -Property $HashTab\n}\n\n#// Export to CSV file\n$CSVOutput | Sort-Object name | Export-CSV $CSVFile -NoTypeInformation\n\n#// End of script\n\n<\/code><\/pre>\n
Advertisement
Now I’m am somewhat sure that this is a logic error that I have the most problem with part because it sort of works. That is it creates the .csv file but only lists up one user multiple times and the groups that user belongs to.<\/p>\n
A fresh pair of eyes to spot the error in my script would be greatly appreciated.<\/p>","upvoteCount":6,"answerCount":7,"datePublished":"2019-07-12T13:43:12.000Z","author":{"@type":"Person","name":"spiceuser-ebjdf","url":"https://community.spiceworks.com/u/spiceuser-ebjdf"},"acceptedAnswer":{"@type":"Answer","text":"
I removed the progress bar (just eye candy you can add it back once it does what you want), and it takes a while to run, but i think this is more what you wanted.<\/p>\n
$DateTime = Get-Date -f \"yyyy-MM\"\n$CSVFile = \"C:\\ADUsers&Groups\" + $DateTime + \".csv\" \n$ADUsers = Get-ADUser -Filter * -Properties displayname\n\n$CSVOutput =\nforeach ($User in $ADUsers) {\n $GroupArr = Get-ADPrincipalGroupMembership -Identity $user.SamAccountName\n\n [pscustomobject]@{\n \"Name\" = $User.Name\n \"Description\" = $User.DisplayName\n \"Groups\" = $GroupArr.name\n }\n}\n\n$CSVOutput | Sort-Object name | Export-CSV $CSVFile -NoTypeInformation\n\n<\/code><\/pre>","upvoteCount":2,"datePublished":"2019-07-12T14:17:23.000Z","url":"https://community.spiceworks.com/t/listing-up-adusers-and-the-groups-they-belong-to/720605/3","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},"suggestedAnswer":[{"@type":"Answer","text":"I have now been creating a scrip that pulls all users from Active Directory, lists them up in alphabetical order and list up the groups each user belongs to.<\/p>\n
Now I have some general programming knowledge but this is the first time I’m making a PowerShell scripts so I have been scouring the interwebs and the code below is what I have come up with so far.<\/p>\n
#// Start of script\n#// Original script found at https://community.spiceworks.com/\n#// topic/381837-script-to-list-all-active-users-and-their-\n#// group-membership-in-a-domain\n#// Modified by arnfridur\n\n#// Get year and month for csv export file\n$DateTime = Get-Date -f \"yyyy-MM\"\n\n#// Set CSV file Name\n$CSVFile = \"C:\\ADUsers&Groups\"+$DateTime+\".csv\"\n\n#// Create empty array for CSV data\n$CSVOutput = @()\n\n#// Get all AD users in the domain\n$ADUsers = Get-ADUser -Filter *\n\n#// Set progress bar variables\n$i=0\n$tot = $ADUsers.count\n\nforeach ($User in $ADUsers) {\n #// Set up progress bar\n $i++\n $status = \"{0:N0}\" -f ($i / $tot * 100)\n Write-Progress -Activity \"Exporting AD Users\" -status \"Processing User $i of $tot : $status% Compleated\" -PercentComplete ($i / $tot * 100)\n\n #// Ensure Groups variable is empty\n $Groups = \"\"\n\n #// Get groups and add to string\n $GroupArr = Get-ADUser -filter {Name -eq $ADUser.Name} | Get-ADPrincipalGroupMembership | select Name\n\n if ($GroupArr) {\n foreach ($Group in $GroupArr) {\n $Groups = $Groups + \",\" + $Group.Name\n }\n $Groups = $Groups.Substring(1,($Groups.Length) -1)\n }\n\n #// Set up hash table and add values\n $HashTab = $null\n $HashTab = [ordered]@{\n \"Name\" = $ADUsers.Name\n \"Description\" = $ADUsers.DisplayName\n \"Groups\" = $Groups\n }\n\n #// Add hash table to CSV data array\n $CSVOutput += New-Object PSObject -Property $HashTab\n}\n\n#// Export to CSV file\n$CSVOutput | Sort-Object name | Export-CSV $CSVFile -NoTypeInformation\n\n#// End of script\n\n<\/code><\/pre>\nNow I’m am somewhat sure that this is a logic error that I have the most problem with part because it sort of works. That is it creates the .csv file but only lists up one user multiple times and the groups that user belongs to.<\/p>\n
A fresh pair of eyes to spot the error in my script would be greatly appreciated.<\/p>","upvoteCount":6,"datePublished":"2019-07-12T13:43:12.000Z","url":"https://community.spiceworks.com/t/listing-up-adusers-and-the-groups-they-belong-to/720605/1","author":{"@type":"Person","name":"spiceuser-ebjdf","url":"https://community.spiceworks.com/u/spiceuser-ebjdf"}},{"@type":"Answer","text":"
Can you re-work the script? It seems off.<\/p>\n
You first query AD to pull all users in, then you query AD again in the foreach loop.<\/p>\n
It looks like the displayname property is not loaded when you query AD<\/p>\n
Then you are using in the second foreach ‘$ADuser.name’ , rather than ‘$user’ as you should in the foreach, really it’s all over the place.<\/p>","upvoteCount":0,"datePublished":"2019-07-12T14:07:08.000Z","url":"https://community.spiceworks.com/t/listing-up-adusers-and-the-groups-they-belong-to/720605/2","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
Yes, what you wrote is more what I want, now I just need it to list all the groups not just one group for each user.
\nI guess I could use foreach object do this …hmmm<\/p>","upvoteCount":0,"datePublished":"2019-07-16T10:55:52.000Z","url":"https://community.spiceworks.com/t/listing-up-adusers-and-the-groups-they-belong-to/720605/4","author":{"@type":"Person","name":"spiceuser-ebjdf","url":"https://community.spiceworks.com/u/spiceuser-ebjdf"}},{"@type":"Answer","text":"
Thank you by the way for the code. It is working almost as intended. The only thing I’m not figuring out is how to make it display the groups in the CSV file. Right now it is only showing up empty.<\/p>\n
I added write-host to the code to see if it was going through stuff and it is. So why I can’t figure out the last bit is frustrating.<\/p>\n
Am I misunderstanding something about how to write the groups down into the field?<\/p>\n
[pscustomobject]@{\n \"Name\" = $User.DisplayName\n \"Groups\" = $GroupArr.Name\n\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2019-07-17T08:23:50.000Z","url":"https://community.spiceworks.com/t/listing-up-adusers-and-the-groups-they-belong-to/720605/5","author":{"@type":"Person","name":"spiceuser-ebjdf","url":"https://community.spiceworks.com/u/spiceuser-ebjdf"}},{"@type":"Answer","text":"read about it here:<\/p>