用于创建CSV文件的PowerShell脚本,列出OU中的所有组和每个组中的成员数

时间:2016-09-28 17:15:38

标签: powershell active-directory

作为AD清理/重组项目的一部分,我正在尝试编写一个脚本,该脚本将输出给定OU中所有组的列表列表以及该组中有多少成员的计数。我可以使用以下内容将数据输出到控制台:

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

如果有人可以帮助我将这个输出变成一个CSV文件,其中一个列中的组名称和第二列中的成员数量,那将非常感激。

1 个答案:

答案 0 :(得分:1)

它应该是这样的

$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

您可能需要将此部分调整为类似"(Get-QADGroupMember $ _。name).count}"我没有测试和确认的任务cmdlet,但选择我展示的方式的属性将是你如何得到你想要的。