Powershell script to display all Users in a Group AD

笑着哭i 提交于 2020-01-04 06:04:45

问题


I have created the below

Get-ADGroup -Filter * -SearchBase "DC=Domain,dc=.com" -properties name, members | 
    Select-Object *,@{Name='Member';Expression={$_.Members -replace '^CN=([^,]+).+$','$1'}} | 
    FT Name, Member -Autosize | 
    out-file c:\text.txt

Ignore Domain and .com I have them populated with my relevant information, but for sake of here removed them.

When I run this it returns what I'm after but when looking at the members within the group they all end with ... and don't show all the members


回答1:


There are a few things to correct. Let's look at them in order. The actual AD query can be simplified: you only need to specify 'Members' as an additional property to retrieve as 'Name' is brought back by default:

Get-ADGroup -Filter * -SearchBase "DC=Domain,dc=.com" -properties members

Given that you only want to output two properties ('Name' and your custom one 'Member'), use your select to retrieve only the ones you want:

Select-Object Name ,@{Name='Member';Expression={$_.Members -replace '^CN=([^,]+).+$','$1'}}

Remove the Format-Table: we have already limited the selection in the previous command. Format cmdlets are designed to format the output to the console window and best practice dictates that they should only be used for that purpose and that they should always be the last element of a pipeline.

Piping all of that to Export-Csv will then produce what you want:

Export-Csv -NoTypeInformation -Path C:\text.csv


来源:https://stackoverflow.com/questions/34224580/powershell-script-to-display-all-users-in-a-group-ad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!