question

KevinAlive-6994 avatar image
1 Vote"
KevinAlive-6994 asked SimonBurbery-9608 answered

PowerShell script to Export All Azure AD groups Starting with "ABC" name and membership of their groups

PowerShell script to Export All Azure AD groups Starting with "ABC" name and membership of their groups

azure-ad-tenant
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, have you looked at this document? It shows how you can export using PowerShell custom commands.


0 Votes 0 ·
soumi-MSFT avatar image
2 Votes"
soumi-MSFT answered JohnathanWest-1439 commented

Hello @KevinAlive-6994, thank you for reaching out. Please do check if the following script helps meet your requirements:

 Connect-AzureAD
 $PathCsv = "C:\temp\GroupMembers.csv"
 $GroupName = Read-Host -Prompt "Enter group name to search"
 $groups = Get-AzureADGroup -SearchString $GroupName
 $groupCount = $groups | measure
 $count = $groupCount.Count
    
    
 $groupMembers = foreach($group in $groups){
     $GroupId = $group.ObjectId
     $GroupName = $group.DisplayName
     Write-Progress -Activity "No of Groups found: $count`
                               Fetching members for GroupName: $GroupName"
     Start-Sleep -Milliseconds 200
     Get-AzureADGroupMember -ObjectId $GroupId | Select-Object -Property @{Name = 'GroupName'; Expression= {$GroupName}}, DisplayName, UserPrincipalName
 }
    
 $groupMembers | Export-Csv -Path $PathCsv -NoTypeInformation -Append


Hope this helps.

Do let us know if this helps and if there are any more queries around this, please do let us know so that we can help you further. Also, please do not forget to accept the response as an Answer; if the above response helped in answering your query.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I would suggest adding an "-All" in line 4 so you know for sure you get all the results returned.
Why have the Start-Sleep at line 14?
I would also suggest adding another proptery in the form of the GroupId because as far as I remembered the groupname doesn't have to be unique technically.

0 Votes 0 ·

I know this is a bit old, but I just wanted to say I came across it and it worked perfectly for me with the same ask, so thanks a bunch!

0 Votes 0 ·
SimonBurbery-9608 avatar image
0 Votes"
SimonBurbery-9608 answered

This script will export all groups with members, along with groups that have no members. You can then filter using excel.
https://www.howdoiuseacomputer.com/index.php/2021/09/12/azure-ad-output-groups-and-members-to-csv/

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.