Im trying to export from specific AD groups the members and their respective departments for each one of them.
I have it like this:
Get-ADGroupMember -Identity "ABC" |
Select-Object Name |
Sort-Object Name |
Export-csv "C:\ABC.csv" -NoTypeInformation
And it did show me all members, but then I`m trying to do another search under each user as the department is a property only for the members.
Maybe under that look for another search:
Get-ADUser -Filter * `
-SearchBase 'ou=abc,ou=Users,dc=corporate,dc=coolguys,dc=org' `
-Properties Name,Department, EmailAddress |
select Name,Department, EmailAddress |
Out-GridView
CodePudding user response:
I don't have a Windows box to test it, but this should do the trick:
Get-AdGroupMember "ABC" -Recursive |
Get-Aduser -Properties Name,Department, EmailAddress |
Select-Object Name,Department, EmailAddress |
Sort-Object -Property Name |
Export-Csv "C:\ABC.csv" -NoTypeInformation -Encoding 'UTF8'
If you use -Recursive
, you should only get objects that are users. If you don't want to use recursive (you're not interested in the members of child groups), then you'd need to slightly alter the code to filter out non-user objects:
Get-AdGroupMember "ABC" |
Where-Object {$_.objectClass -eq 'User'} |
Get-Aduser -Properties Name,Department, EmailAddress |
Select-Object Name,Department, EmailAddress |
Sort-Object -Property Name |
Export-Csv "C:\ABC.csv" -NoTypeInformation -Encoding 'UTF8'
I added -Encoding 'UTF8'
to the Export-Csv
call, to make sure you have no character encoding issues in the export file.