Forum Discussion
omc_st2022
Aug 17, 2022Copper Contributor
PowerShell - Get members of multiple groups
Hi, I'm trying to get the members of multiple groups listed is a CSV file. Here is an example of a script I've been trying to use to achieve the above: $csv = Import-Csv "C:\Temp\Groups\...
- Aug 18, 2022
Let me put some of the information you've provided together into a working example.
Here's our CSV file layout:
Here's our script for pulling the members:
(Import-Csv -Path "C:\Temp\Groups\testgroups.csv").GroupName | ForEach-Object { Get-AzureADGroup -Filter "displayName eq '$_'" } | ForEach-Object { $Group = $_; $Group | Get-AzureADGroupMember | Select-Object @{n="GroupName"; e = { $Group.DisplayName; }}, ObjectId, UserPrincipalName, DisplayName; }And here's the results (using my own example group names):
Cheers,
Lain
omc_st2022
Aug 17, 2022Copper Contributor
Harm_Veenstra Thanks for the reply, after the script is run $groupname is showing the last group name in the CSV, same with $line.
My csv looks like this:
| GroupName |
| TESTGroup1 |
| TESTGroup2 |
Aug 17, 2022
Not sure why you're using CSV with one column 😉 a get-content of a txt file containing one line per group is the same... If you run this manually, what's the output?
(Get-AzureADGroup | Where-Object {$_.DisplayName -eq $testgroup1}
(Get-AzureADGroup | Where-Object {$_.DisplayName -eq $testgroup1}