如何在Powershell中格式化多个条目

时间:2013-06-04 16:18:59

标签: powershell active-directory

我正在使用以下代码获取Powershell中的直接下属列表:

import-module activedirectory

$identity = read-host "Enter username:"

Get-ADUser $identity -Properties directReports | select-object {$_.directReports} | export-csv "DirectReports-$identity.csv"

这会将所有直接报告输出到一行,例如“CN = User1,CN = User2,CN = User3”.....

如何修改它以便将每个用户输出到自己的行?

2 个答案:

答案 0 :(得分:0)

我确信这不是最干净的解决方案:

Get-ADUser $identity -Properties directReports | select-object -expandproperty directReports|foreach-object{$_.split(",")[0].replace("CN=","")}| export-csv "DirectReports-$identity.csv"

您将在CSV文件中单独获取每个人的姓名()。

答案 1 :(得分:0)

Get-ADUser $identity -Properties directReports | 
Select-Object Name,@{n='DirectReports';e={$_.directReports.Split(',CN=',[System.StringSplitOptions]::RemoveEmptyEntries) -join ';'}} | 
Export-Csv "DirectReports-$identity.csv"