简体   繁体   中英

exporting AD users displayName for selected groups only - powershell

I am new to powershell so please excuse me if the answer is quite simple. I am trying to get user list sorted by selected AD groups and export that to table or csv at least. Due to the fact that:

Get-ADGroupMember -Identity "TestGroupName"

... gives me only user IDs for my AD, I used below:

Get-ADGroupMember -Identity "TestGroupName" | Get-ADObject -Properties displayName 

This works perfectly but I do not want to type manually each group there so I decided to first export groups that I need which are beginning with "Test":

Get-ADGroup -Filter "name -like 'Test*'" |Select-Object Name | Export-csv -path \Groups.csv

Now I want to use information from Groups.csv to list all user displayName for groups listed in Groups.csv so I tried something like that:

Import-Csv -Path .\Groups.csv | Get-ADGroupMember ForEach($Name in $Groups) | Get-ADObject -Properties displayName | Export-csv -path \UsersByGroups.csv

unfortunately it does not work properly maybe because I still do not get exactly how to use ForEach

Can someone with more experience have a look and help?

Thanks! Maciej

Just pipe the groups output by Get-ADGroup -Filter... directly to Get-ADGroupMember :

Get-ADGroup -Filter "name -like 'Test*'" |Get-ADGroupMember |Get-ADObject -Properties displayName 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM