Powershell将AD用户导出到csv

时间:2016-09-09 10:13:37

标签: powershell active-directory export export-to-csv

我试图通过PowerShell将AD用户导出到csv文件。现在,一个字段只需要包含值的第一个字母。例如:姓名 - 凯文,名字缩写 - K。

我尝试过使用子字符串,但显然我做错了:

PS C:\Users\Administrator> Get-ADUser -Filter * -Properties * | Select -Property substring.surname(1,1),surname,givenName,samaccountname,givenN
ame,Mail,Department | Export-CSV "C:\Export\Users.csv" -NoTypeInformation

显然我对powershell来说还是一个新手。

2 个答案:

答案 0 :(得分:2)

这里有几个问题:

1)您选择“GivenName”两次 - PowerShell不会允许: enter image description here

2)你想要子串(0,1) - 从位置0开始并沿着计数1个字母。

3)这应该做你想做的事情:

Get-ADUser -Filter * -Properties * | `
Select @{n='initial';e={$_.surname.substring(0,1)}},surname,givenName,samaccountname,Mail,Department | `
Export-CSV "C:\installs\Users.csv" -NoTypeInformation

基本上,你每次都在创建和回收一个小的属性数组 - 这允许你设置一个值(子串),然后将链返回到选择,然后返回到你的CSV文件:)

(也许值得注意的是,你选择姓氏的第一个字母,但在你的问题中,你要求从凯文获得K - 你可能想从一个姓名字段中选择。)

其他说明:

-filter * -properties *计算成本非常高 - 您为所有用户获取了所有字段,只选择了一些。尝试列出属性的方式与选择方式相同。

select -property - -property开关是冗余和可选的 - 只需使用Select stuff,otherstuff即可执行相同的操作并且更易于阅读

答案 1 :(得分:0)

如果您觉得这个过程很简单,您还可以尝试使用CJWdev工具或Lepide活动目录查询工具。它们都是免费的,有助于快速获取此类AD报告。

相关问题