Export members for a list of security groups from AAD

%3CLINGO-SUB%20id%3D%22lingo-sub-2670716%22%20slang%3D%22en-US%22%3EExport%20members%20for%20a%20list%20of%20security%20groups%20from%20AAD%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2670716%22%20slang%3D%22en-US%22%3E%3CP%3EHi.%20I%20need%20to%20download%20the%20members%20of%201000%20security%20groups%20in%20AAD.%20Can%20I%20export%20members%20(name%2C%20email%2C%20upn)%20for%20a%20specific%20list%20of%20security%20groups%20with%20name%20begins%20with%20'FP3'%20or%20from%20a%20csv%20file%3F%20Tried%20using%20the%20PS%20command%20from%20this%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fanswers%2Fquestions%2F139633%2Fpowershell-script-to-export-all-azure-ad-groups-st-1.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Epost%3C%2FA%3E%20but%20the%20script%20seems%20incorrect%20as%20line%203%20is%20asking%20to%20key%20just%20one%20security%20group.%20Tried%20using%20wildcard%2C%20FP3*%20but%20doesn't%20seem%20to%20work%20too.%20Thanks%20in%20advanced.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2670716%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%20AD%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2671606%22%20slang%3D%22en-US%22%3ERe%3A%20Export%20members%20for%20a%20list%20of%20security%20groups%20from%20AAD%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2671606%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1080715%22%20target%3D%22_blank%22%3E%40filzah%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20can%20offer%20you%20this%20script%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24Groups%20%3D%20Get-AzureADGroup%20-SearchString%20FP3%0Aforeach%20(%24group%20in%20%24groups)%20%7B%0AGet-AzureADGroupMember%20-ObjectId%20%24group.ObjectId%20%7C%20fl%20DisplayName%2CUserPrincipalName%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20can%20simply%20change%20the%20Searchstring.%20I%20used%20your%20%22FP3%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDon't%20forget%20to%20export%20it%20into%20your%20format%20(CSV%20or%20whatever)%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20give%20me%20a%20little%20feedback%2C%20if%20I%20met%20your%20requirements%20%3A'D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBest%20regards%2C%3CBR%20%2F%3ESchnittlauch%3C%2FP%3E%3CP%3E%22First%2C%20No%20system%20is%20safe.%20Second%2C%20Aim%20for%20the%20impossible.%20Third%2C%20no%20Backup%2C%20no%20Mercy%22%20-%20Schnittlauch%3C%2FP%3E%3CP%3EMy%20answer%20helped%20you%3F%20Don't%20forget%20to%20leave%20a%20like.%20Also%20mark%20the%20answer%20as%20solved%20when%20your%20problem%20is%20solved.%20%3A)%3C%2Fimg%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2671609%22%20slang%3D%22en-US%22%3ERe%3A%20Export%20members%20for%20a%20list%20of%20security%20groups%20from%20AAD%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2671609%22%20slang%3D%22en-US%22%3EWait%20a%20second%2C%20didn't%20get%20it.%20Do%20the%20groups%20have%20the%20name%20FP3%20or%20the%20users%3F%20%3AD%3C%2Fimg%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2671610%22%20slang%3D%22en-US%22%3ERe%3A%20Export%20members%20for%20a%20list%20of%20security%20groups%20from%20AAD%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2671610%22%20slang%3D%22en-US%22%3EFYI%20this%20script%20is%20aswell%20good%2C%20if%20you%20add%20the%3CBR%20%2F%3E-all%3CBR%20%2F%3Ein%20line%204%3CBR%20%2F%3E%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fanswers%2Fquestions%2F139633%2Fpowershell-script-to-export-all-azure-ad-groups-st-1.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fanswers%2Fquestions%2F139633%2Fpowershell-script-to-export-all-azure-ad-groups-st-1.html%3C%2FA%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2676892%22%20slang%3D%22en-US%22%3ERe%3A%20Export%20members%20for%20a%20list%20of%20security%20groups%20from%20AAD%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2676892%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3EThis%20is%20the%20same%20script%20that%20you%20highlighted%20and%20has%20been%20adjusted%20a%20little%20bit.%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhen%20prompted%2C%20simply%20provide%20the%20name%20with%20which%20your%20group%20starts.%20e.g.%20FP3.%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20normal%20limit%20on%20returned%20results%20is%20100%20objects%2C%20which%20has%20been%20increased%20to%20max%20or%20%22All%20%24True%22%20in%20this%20case.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3BConnect-AzureAD%20%26nbsp%3B%24PathCsv%20%3D%20%22C%3A%5CGroupMembers.csv%22%20%26nbsp%3B%24GroupName%20%3D%20Read-Host%20-Prompt%20%22Enter%20group%20name%20to%20search%22%20%26nbsp%3B%24groups%20%3D%20Get-%3CA%20href%3D%22https%3A%2F%2Fbit.ly%2F3kzxQvh%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3EAzureADGroup%3C%2FA%3E%20-SearchString%20%24GroupName%20-all%20%24true%20%26nbsp%3B%24groupCount%20%3D%20%24groups%20%7C%20measure%20%26nbsp%3B%24count%20%3D%20%24groupCount.Count%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%24groupMembers%20%3D%20foreach(%24group%20in%20%24groups)%7B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%24GroupId%20%3D%20%24group.ObjectId%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%24GroupName%20%3D%20%24group.DisplayName%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BWrite-Progress%20-Activity%20%22No%20of%20Groups%20found%3A%20%24count%60%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BFetching%20members%20for%20GroupName%3A%20%24GroupName%22%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BStart-Sleep%20-Milliseconds%20200%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BGet-AzureADGroupMember%20-ObjectId%20%24GroupId%20-All%20%24true%20%7C%20Select-Object%20-Property%20%40%7B%20Name%20%3D%20'GroupName'%3B%20Expression%3D%20%7B%20%24GroupName%7D%20%7D%20%2C%20DisplayName%2C%20UserPrincipalName%20%26nbsp%3B%7D%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%24groupMembers%20%7C%20Export-Csv%20-Path%20%24PathCsv%20-NoTypeInformation%20-Force%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOriginal%20script%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fanswers%2Fquestions%2F139633%2Fpowershell-script-to-export-all-azure-ad-groups-st-1.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fanswers%2Fquestions%2F139633%2Fpowershell-script-to-export-all-azure-ad-groups-st-1.html%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Hi. I need to download the members of 1000 security groups in AAD. Can I export members (name, email, upn) for a specific list of security groups with name begins with 'FP3' or from a csv file? Tried using the PS command from this post but the script seems incorrect as line 3 is asking to key just one security group. Tried using wildcard, FP3* but doesn't seem to work too. Thanks in advanced. 

8 Replies

Hi @filzah 

 

I can offer you this script:

 

$Groups = Get-AzureADGroup -SearchString FP3
foreach ($group in $groups) {
Get-AzureADGroupMember -ObjectId $group.ObjectId | fl DisplayName,UserPrincipalName
}

 

You can simply change the Searchstring. I used your "FP3"

 

Don't forget to export it into your format (CSV or whatever) 

 

Please give me a little feedback, if I met your requirements :'D

 

Best regards,
Schnittlauch

"First, No system is safe. Second, Aim for the impossible. Third, no Backup, no Mercy" - Schnittlauch

My answer helped you? Don't forget to leave a like. Also mark the answer as solved when your problem is solved. :)

Wait a second, didn't get it. Do the groups have the name FP3 or the users? :D

This is the same script that you highlighted and has been adjusted a little bit. 

 

When prompted, simply provide the name with which your group starts. e.g. FP3. 

The normal limit on returned results is 100 objects, which has been increased to max or "All $True" in this case. 

 

 Connect-AzureAD  $PathCsv = "C:\GroupMembers.csv"  $GroupName = Read-Host -Prompt "Enter group name to search"  $groups = Get-AzureADGroup -SearchString $GroupName -all $true  $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 -All $true | Select-Object -Property @{​​​​​​​Name = 'GroupName'; Expression= {​​​​​​​$GroupName}​​​​​​​}​​​​​​​, DisplayName, UserPrincipalName  }​​​​​​​       $groupMembers | Export-Csv -Path $PathCsv -NoTypeInformation -Force

 

 

Original script:

 

https://docs.microsoft.com/en-us/answers/questions/139633/powershell-script-to-export-all-azure-ad-g...

Hi @Schnittlauch Thanks so much for replying. Almost there! I used the code below but somehow the results are incomplete. Would you know why and how I can rectify this?

Connect-AzureAD
$groups=Get-AzureADGroup -SearchString FP3_Share_IS_
$resultsarray =@()
ForEach ($group in $groups){
    $members = Get-AzureADGroupMember -ObjectId $group.ObjectId -All $true 
    ForEach ($member in $members){
       $UserObject = new-object PSObject
       $UserObject | add-member  -membertype NoteProperty -name "Group Name" -Value $group.DisplayName
       $UserObject | add-member  -membertype NoteProperty -name "Member Name" -Value $member.DisplayName
       $UserObject | add-member  -membertype NoteProperty -name "ObjType" -Value $member.ObjectType
       $UserObject | add-member  -membertype NoteProperty -name "UserType" -Value $member.UserType
       $UserObject | add-member  -membertype NoteProperty -name "UserPrinicpalName" -Value $member.UserPrincipalName
       $resultsarray += $UserObject
    }
}
$resultsarray | Export-Csv -Encoding UTF8  -Delimiter ";" -Path "C:\scripts\output.csv" -NoTypeInformation

 

Hi. I keyed FP3 but results were inaccurate as it is showing other groups that doesn't begin with FP3 too.

@filzah 

Here is an updated Powershell script, It will now only look for the Groups starting with input value.
In this case just key in FP3 and it will only bring groups that begin with FP3.
 
Connect-AzureAD
$PathCsv = "C:\temp\GroupMembers.csv"
$GroupName = Read-Host -Prompt "Enter group Displayname to search"
$groups = Get-AzureADGroup -All $true | Where-object {$_.DisplayName -like "$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 -All $true | Select-Object -Property @{Name = 'GroupName'; Expression= {$GroupName}}, DisplayName, UserPrincipalName
}

$groupMembers | Export-Csv -Path $PathCsv -NoTypeInformation