Powershell

TUTORIAL: Get all groups with members in Office 365 Powershell

Getting all groups with members in office 365 Powershell is a painful process. With this post I’ll try to make it as easy as possible for you.

I’ve taken Alan byrne’s script how to export all groups with members, because I noticed a lot of users ask where the export file was placed. It will now be placed in C:\scripts\DistributionGroupMembers.csv. For more about the script and how to apply filters, please check out this blog post.

This script will export all Distribution Groups and their members from Office 365 to a CSV file which you can manipulate in Excel.

The script outputs data as a CSV file in the following format:

‘Distribution Group DisplayName,Distribution Group Email,Member DisplayName, Member Email, Member Type’

Also, if you’d like to apply a filter try the following:

Change this part:

#Get all Distribution Groups from Office 365  
$objDistributionGroups = Get-DistributionGroup  -ResultSize Unlimited

To:

#Get all Distribution Groups from Office 365  
$objDistributionGroups = Get-DistributionGroup -Filter {Name -like "*VARIABLE TO FILTER ON*"} -ResultSize Unlimited

The Get all groups with members in Office 365 Powershell full script

################################################################################################################################################################ 
# Script accepts 2 parameters from the command line 
# 
# Office365Username - Optional - Administrator login ID for the tenant we are querying 
# Office365Password - Optional - Administrator login password for the tenant we are querying 
# 
# 
# To run the script 
# 
# .\Get-DistributionGroupMembers.ps1 [-Office365Username admin@xxxxxx.onmicrosoft.com] [-Office365Password Password123]
# 
# 
# Author:                 Alan Byrne 
# Version:                 2.0 
# Last Modified Date:     16/08/2014 
# Last Modified By:     Bas Wijdenes info@baswijdenes.com
################################################################################################################################################################ 
#Accept input parameters 
Param( 
    [Parameter(Position=0, Mandatory=$false, ValueFromPipeline=$true)] 
    [string] $Office365Username, 
    [Parameter(Position=1, Mandatory=$false, ValueFromPipeline=$true)] 
    [string] $Office365Password 
) 

#Constant Variables 
$OutputFile = "C:\scripts\DistributionGroupMembers.csv"   #The CSV Output file that is created, change for your purposes 
$arrDLMembers = @{} 
#Remove all existing Powershell sessions 
Get-PSSession | Remove-PSSession 

#Did they provide creds?  If not, ask them for it.
if (([string]::IsNullOrEmpty($Office365Username) -eq $false) -and ([string]::IsNullOrEmpty($Office365Password) -eq $false))
{
    $SecureOffice365Password = ConvertTo-SecureString -AsPlainText $Office365Password -Force     

    #Build credentials object 
    $Office365Credentials  = New-Object System.Management.Automation.PSCredential $Office365Username, $SecureOffice365Password 
}
else
{
    #Build credentials object 
    $Office365Credentials  = Get-Credential
}
#Create remote Powershell session 
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $Office365credentials -Authentication Basic –AllowRedirection         

#Import the session 
Import-PSSession $Session -AllowClobber | Out-Null          

#Prepare Output file with headers 
Out-File -FilePath $OutputFile -InputObject "Distribution Group DisplayName,Distribution Group Email,Member DisplayName, Member Email, Member Type" -Encoding UTF8 

#Get all Distribution Groups from Office 365 
$objDistributionGroups = Get-DistributionGroup  -ResultSize Unlimited 

#Iterate through all groups, one at a time     
Foreach ($objDistributionGroup in $objDistributionGroups) 
{     

    write-host "Processing $($objDistributionGroup.DisplayName)..." 

    #Get members of this group 
    $objDGMembers = Get-DistributionGroupMember -Identity $($objDistributionGroup.PrimarySmtpAddress) 

    write-host "Found $($objDGMembers.Count) members..." 

    #Iterate through each member 
    Foreach ($objMember in $objDGMembers) 
    { 
        export-csv 'C:\scripts\list.csv' -InputObject "$($objDistributionGroup.DisplayName),$($objDistributionGroup.PrimarySMTPAddress),$($objMember.DisplayName),$($objMember.PrimarySMTPAddress),$($objMember.RecipientType)" -Encoding UTF8 -append 
        write-host "`t$($objDistributionGroup.DisplayName),$($objDistributionGroup.PrimarySMTPAddress),$($objMember.DisplayName),$($objMember.PrimarySMTPAddress),$($objMember.RecipientType)"
    }
} 

#Clean up session 
Get-PSSession | Remove-PSSession

Also for the Technet post click here.


A little extra

This post contains PowerShell. Would you like to learn the basics better? I have created a new website to learn basic PowerShell in an ’emulator’ environment.
Click here to go learn Basic PowerShell.

Published by

Bas

My name is Bas Wijdenes and I work full-time as a Services Engineer. In my spare time I write about the error messages that I encounter during my work. Furthermore, I am currently occupied with Office 365, Azure infra, and PowerShell for automating daily tasks.

Leave a Reply

Your email address will not be published. Required fields are marked *