Generate list of all members of specific AD groups with properties

Hello Guys,

 

I’m working on a script which will be used to audit members of all Ad groups with a specific string in the name. I want to generate a table with the group name and all members underneath - which will include their full name, title, department etc.

 

I have got most of this working but i know need to output the results to a spreadsheet and when i try to pipe format table into export csv, i get junk (i assume export csv doesn’t support input from format table). I also cannot skip the format table function as i need it to format the information. Can anyone advise on how i could save the output in a way which will look the same as the console output when the script is run?

I’ll also add that i am new to powershell so i may be missing something obvious. I’ve also removed sensitive data such as ou path and search string.

Here is the code:

 

 

$Groups = Get-ADGroup -SearchBase "OU=x,DC=x,DC=x,DC=x" -Filter "name -like '*search-string*'"


Foreach($Group in $Groups) {
    echo $Group
    Get-ADGroupMember -Identity $Group | Get-ADObject -Properties Name, Title, Department| Format-Table Name, Title, Department 

}

Thanks

A

You could change it just slightly. The select object will create a new object with only those properties so your export should be cleaner.

[pre]

Get-ADGroupMember -Identity $Group | Get-ADObject -Properties Name, Title, Department| Select-Object -Property Name, Title, Department | Export-csv -path somepath.csv

[/pre]

I think you can take out the “format-table” & put in “Select” or “Select-Object” instead when exporting to CSV.

Then, add the “| Export-CSV FILENAME.CSV -NoTypeInformation” to the end of the script to create the CSV file.

Here’s one I wrote. It’s a bit different than yours. I figured how to insert the group name into the CSV file name to get a unique CSV for each group. The only drawback is that you would need to enter the exact group name for my script to work, although you could probably change that part if you don’t like it.

[pre]

Import-Module ActiveDirectory
$Group = Read-Host -Prompt ‘Enter the group name’
Get-ADGroupMember -Identity $Group | Get-ADUser -properties mail, title, department| Select Name,samAccountName,Mail,Title, Department `
| sort Name | Export-Csv GroupMembers_$($Group).csv -NoTypeInformation

[/pre]

 

Alicia

Format-Table really kills the object, its meant only for console outputs. Same is the behavior of all Format-* cmdlets.
Export-Csv needs object and follow the Select-Object option mentioned by others above.