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
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
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_st2022Aug 19, 2022Copper Contributor
LainRobertson Goodness me your script does work! It's structured different to what I was using, but it works whereas the original script I was trying to run does not. I guess my skills are still primitive, I shall reside in the temple and practice my PowerShell-Fu some more.
Huge thanks to you and Harm_Veenstra for helping me out with this, its nice to know people are out here offering free help to those in need.
All the best, cheers