Members of an Office 365 group can retrieved and exported in to a CSV file using Office command-line capabilities.
I recently had an requirement in a large enterprise setup to retrieve the members list of specific set of top level groups and export them in to a CSV file for a auditing purpose. This could be a common requirement, hence here it goes as a quick blog.
We will cover up two scenarios here.
- Retrieve and export members of an specific Office 365 group (Part01)
- Retrieve and export members of all Office 365 groups (Part 02)
Let’s cover up the 1st scenario. Just log in to Office 365 portal and head on to Admin Center page –> Groups –> click on the respective group and copy the group mail
Now let’s run PowerShell as an Administrator and make sure the execution policy is set to remote sign (if you haven’t yet, run this on the device you are going to do this task – set-executionpolicy remotesigned)
Here are the next few lines piece by piece. Also I have given the full script of the whole thing at the end.
$Credential = Get-Credential
Enter the Admin credentials for your tenant here.
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection
Import-PSSession $Session -DisableNameChecking
Import command might take a few seconds just to load up the modules as shown below.
Get-UnifiedGroup -Identity "InternalIT@mantoso.onmicrosoft.com" | Get-UnifiedGroupLinks -LinkType Member
Now, replace the “Identity” parameter here and run this line.
Get-UnifiedGroup -Identity "InternalIT@mantoso.onmicrosoft.com" | Get-UnifiedGroupLinks -LinkType Member
And here’s the command to export what we retrieved. Again, replace the <Identity> and <Path> here before running.
Get-UnifiedGroup -Identity "InternalIT@mantoso.onmicrosoft.com" | Get-UnifiedGroupLinks -LinkType Member | Select DisplayName,PrimarySmtpAddress | Export-CSV "C:\Exports\MembersList.csv" -NoTypeInformation
And, after you running the last line, you should now be able to see the CSV file created under the given path.
Just open it and it should have all the entries as shown below in my example.
Here’s the full script of the same thing. You can run it all at once as well.
### Get O365 Credentials $Credential = Get-Credential ### Provision the session $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection ### Import session Import-PSSession $Session -DisableNameChecking ### Retrieve Members of Office 365 Group Get-UnifiedGroup -Identity "InternalIT@mantoso.onmicrosoft.com" | Get-UnifiedGroupLinks -LinkType Member ### Remove the session Remove-PSSession $Session ### Export Get-UnifiedGroup -Identity "InternalIT@mantoso.onmicrosoft.com" | Get-UnifiedGroupLinks -LinkType Member | Select DisplayName,PrimarySmtpAddress | Export-CSV "C:\Exports\MembersList.csv" -NoTypeInformation
DISCLAIMER NOTE: This is an enthusiast post and is not sponsored by Microsoft or any other vendor. Please do not copy/duplicate the content of the post unless you are authorized by me to do so.
### Get O365 Credentials $Credential = Get-Credential ### Provision the session $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection ### Import session Import-PSSession $Session -DisableNameChecking ### Retrieve Members of Office 365 Group Get-UnifiedGroup -Identity "InternalIT@mantoso.onmicrosoft.com" | Get-UnifiedGroupLinks -LinkType Member ### Remove the session Remove-PSSession $Session ### Export Get-UnifiedGroup -Identity "InternalIT@mantoso.onmicrosoft.com" | Get-UnifiedGroupLinks -LinkType Member | Select DisplayName,PrimarySmtpAddress | Export-CSV "C:\Exports\MembersList.csv" -NoTypeInformation