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
LainRobertson
Aug 17, 2022Silver Contributor
Something's not lining up with the data held in the CSV and the Get-AzureADGroup command on line 4.
Essentially, line 4 is not producing any matching groups, which is the only way your $objectid variable can end up being $null.
Maybe the wrong names are in the CSV. Maybe there's no header or the header is not named GroupName - I have no idea as we can't see any data from your CSV file.
Anyhow, if you solve whatever the mismatch is, you'll solve the ObjectId is $null error.
Cheers,
Lain