MattZ
MattZ

Reputation: 149

PowerShell script to create CSV file listing all groups in OU and # of members in each group

As part of an AD cleanup/restructuring project I am trying to write a script that will output a list list of all groups in a given OU along with a count of how many members are in the group. I can get the data output to console using the following:

$OU = "mydomain.com/Groups/SecurityGroups"
$GroupList = @()
$GroupList = Get-QADGroup -searchroot $OU -GroupType Security -SizeLimit 0
$GroupList |
ForEach-Object { Write-Host $_.Name, (Get-QADGroupMember $_).count } 

If anyone could help me get this output into a CSV file with the group name in one column and the # of members in a second column, it would be much appreciated.

Upvotes: 0

Views: 1061

Answers (1)

Noah Sparks
Noah Sparks

Reputation: 1762

it should be something like this

$OU = "mydomain.com/Groups/SecurityGroups"
$GroupList = @()
$GroupList = Get-QADGroup -searchroot $OU -GroupType Security -SizeLimit 0
$GroupList | Select Name,@{n='count';e={(Get-QADGroupMember $_).count}} | Export-CSV C:\csvpath.csv -NoTypeInformation

you may need to adjust this part though to something like "(Get-QADGroupMember $_.name).count}" I don't have the quest cmdlets to test and confirm, but selecting the properties in the fashion I showed would be how you get what you want.

Upvotes: 1

Related Questions