Wednesday, 8 June 2016

List Office 365 Group Members using Powershell

The post helps you to list office 365 group (not distribution group) members by using powershell script. We can list all the office 365 groups by using the powershell cmdlet Get-UnifiedGroup and its group members by Get-UnifiedGroupLinks cmdlet.

Note: Before proceed, Connect Exchange Online Remote PowerShell.

The following command lists all the office 365 groups.
Get-UnifiedGroup | Select DisplayName,GroupType,PrimarySmtpAddress

List Office 365 Group Members

We can use the powershell cmdlet Get-UnifiedGroupLinks to view the members of an existing group. The key parameters for this cmdlet are:

Identity – the alias of the group
LinkType – Members, Owners, or Subscribers. Required.

Use the below powershell command to select members of a single office 365 group.
Get-UnifiedGroupLinks -Identity '<group-name>' -LinkType Members
If you want to list members of all the office 365 groups, first, we need to get the results of Get-UnifiedGroup, then we can pipe the output to ForEach-Object and get members for all the office 365 groups.
$Groups = Get-UnifiedGroup -ResultSize Unlimited
$Groups | ForEach-Object {
$group = $_
Get-UnifiedGroupLinks -Identity $group.Name -LinkType Members | ForEach-Object {
      New-Object -TypeName PSObject -Property @{
       Group = $group.DisplayName
       Member = $_.Name
       EmailAddress = $_.PrimarySMTPAddress
       RecipientType= $_.RecipientType
}}}

Export All Office 365 Group Members to CSV

We can export powershell output into CSV file using Export-CSV cmdlet. The following command exports all the office 365 group members to CSV file.
$Groups = Get-UnifiedGroup -ResultSize Unlimited
$Groups | ForEach-Object {
$group = $_
Get-UnifiedGroupLinks -Identity $group.Name -LinkType Members | ForEach-Object {
      New-Object -TypeName PSObject -Property @{
       Group = $group.DisplayName
       Member = $_.Name
       EmailAddress = $_.PrimarySMTPAddress
       RecipientType= $_.RecipientType
}}} |

Export-CSV "C:\\Office365GroupMembers.csv" -NoTypeInformation -Encoding UTF8

Advertisements
Advertisements

No comments:

Post a Comment