Forum Discussion
PowerShell - Get members of multiple 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
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, 2022Not 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}- omc_st2022Aug 18, 2022Copper ContributorI did actually try this with a txt file listing the group names without a header, and got the same result.
If I run the following command:
(Get-AzureADGroup | Where-Object {$_.DisplayName -eq $groupname})
I get nothing, the cursor just jumps down to the next line. Its like this line is failing to obtain the objectid and append it to the variable, then the following line (Get-AzureADGroupMember -ObjectId $objectid) is failing as $objectid is null.- Aug 18, 2022Ran your script in my test tenant with only sg-finance as group in the csv, it just works?!
C:\Users\HarmV> $csv = Import-Csv "C:\Temp\Groups\testgroups.csv"
C:\Users\HarmV> $csv
GroupName
---------
sg-finance
C:\Users\HarmV> foreach ($line in $csv){
>> $groupname = $line.GroupName
>> $objectid = (Get-AzureADGroup | Where-Object {$_.DisplayName -eq $groupname}).ObjectId
>> Get-AzureADGroupMember -ObjectId $objectid | select DisplayName,UserPrincipalName | Export-Csv -Path "C:\Temp\Groups\testmembers.csv" -NoTypeInformation -Append
>> }
C:\Users\HarmV> cat C:\temp\groups\testmembers.csv
"DisplayName","UserPrincipalName"
"Pradeep Gupta","email address removed for privacy reasons"
"Debra Berger","email address removed for privacy reasons"
"Megan Bowen","email address removed for privacy reasons"
"Diego Siciliani","email address removed for privacy reasons"
Is the displayname of your group different than the groupname?
- LainRobertsonAug 17, 2022Silver Contributor
Stripping things right back to basics, taking variables and client-side filtering out completely, can you actually see one of those groups through running a basic command like this:
Get-AzureADGroup -Filter "displayName eq 'TESTGroup1'";Cheers,
Lain
- omc_st2022Aug 18, 2022Copper Contributor
LainRobertson If I run that command it returns the group objectid, display name and description of the group correctly.
So due to Get-AzureADGroupMember not supporting -identity GroupName (like Get-ADGroupMember does), we're having to locate the objectid for each group and that's the bit i'm stuck at? It's like somethings wonky with the syntax in line 4, nullifying the $objectid variable:
$objectid = (Get-AzureADGroup | Where-Object {$_.DisplayName -eq $groupname}).ObjectId