Forum Discussion

omc_st2022's avatar
omc_st2022
Copper Contributor
Aug 17, 2022
Solved

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\testgroups.csv"
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
}

 

However, this script fails with the following error:
  

Get-AzureADGroupMember : Cannot bind argument to parameter 'ObjectId' because it is null.
At line:4 char:38
+ Get-AzureADGroupMember -ObjectId $objectid | select DisplayName,U ...
+ ~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-AzureADGroupMember], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Open.AzureAD16.PowerShell.GetGroupMembers

  
I'm assuming its failing on line 4 of the script, and not pulling the objectid for each group?

 

Any help would be greatly appreciated.
 
Thanks

  • omc_st2022 

     

    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

12 Replies

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    omc_st2022 

     

    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

    • omc_st2022's avatar
      omc_st2022
      Copper Contributor
      Thanks Lain, appreciate your input. I'm no PS expert, I've posted my CSV layout, hopefully I'm not doing something silly here. BTW my PS version is 5.1.19041.1682 if that helps. Cheers
  • After starting the script and receiving the error, how are the $line and $groupname variables at that moment?
    • omc_st2022's avatar
      omc_st2022
      Copper 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
      • Harm_Veenstra's avatar
        Harm_Veenstra
        MVP
        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}

Resources