Created
June 4, 2025 17:47
-
-
Save griffeth-barker/fe95114d2037f4ff874a3f137854cd34 to your computer and use it in GitHub Desktop.
Export a distribution group's information from Exchange Online
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Export-DistributionGroupInfo { | |
<# | |
.SYNOPSIS | |
This function exports the information for an Exchange Online distribution group. | |
.DESCRIPTION | |
This function takes in an array of distribution group PrimarySmtpAddresses. Each group is looked up and their information | |
is condensed to a single .csv file which is exported to your Downloads directory. | |
.PARAMETER Groups | |
The PrimarySmtpAddress(es) of the distribution group(s) for which you'd like to export information. | |
Required : true | |
ValueFromPipeline : true | |
Type : array | |
.PARAMETER RevealInFileSystem | |
Reveal the output file in File Explorer (requires Windows platform). | |
Required : false | |
ValueFromPipeline : false | |
Type : switch | |
.PARAMETER OpenOutputFile | |
Open the output file in the default application for .csv files. | |
Required : false | |
ValueFromPipeline : false | |
Type : switch | |
.INPUTS | |
PrimarySmtpAddress(es) | |
.OUTPUTS | |
DgInfoExport_<PrimarySmtpAddress>.csv | |
.NOTES | |
Author : Griff Barker ([email protected]) | |
Change Date : 2025-06-04 | |
Purpose/Change : Initial development | |
.EXAMPLE | |
# Dot-source the function then run it to export a single group's information | |
. "C:\Path\To\Export-DistributionGroupInfo.ps1" | |
Export-DistributionGroupInfo.ps1 -Groups @( | |
'[email protected]' | |
) | |
.EXAMPLE | |
# Dot-source the function then run it to export multiple groups' information | |
. "C:\Path\To\Export-DistributionGroupInfo.ps1" | |
Export-DistributionGroupInfo.ps1 -Groups @( | |
'[email protected]', | |
'[email protected]', | |
'[email protected]' | |
) | |
.LINK | |
https://gist.github.com/griffeth-barker/fe95114d2037f4ff874a3f137854cd34 | |
#> | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory = $true, ValueFromPipeline = $true)] | |
[array] | |
$Groups, | |
[Parameter(Mandatory = $false, ParameterSetName = 'PostExportAction')] | |
[switch] | |
$RevealInFileSystem, | |
[Parameter(Mandatory = $false, ParameterSetName = 'PostExportAction')] | |
[switch] | |
$OpenOutputFile | |
) | |
begin { | |
#Requires -Modules ExchangeOnlineManagement | |
Write-Debug "Importing module: 'ExchangeOnlineManagement'" | |
Import-Module -Name ExchangeOnlineManagement | |
Write-Debug "Creating system collection: 'reportData'" | |
$reportData = [System.Collections.Generic.List[psobject]]::new() | |
if (-not (Get-ConnectionInformation)) { | |
Write-Debug "Connecting to Exchange Online" | |
try { | |
Write-Debug "If the function appears to be hung here, there may be a pop-up window in the background waiting for you to log in to Exchange Online." | |
Connect-ExchangeOnline -ShowBanner:$false -ErrorAction Stop | |
Write-Debug $(Get-ConnectionInformation) | |
} | |
catch { | |
Write-Error "Failed to connect to Exchange Online" | |
Write-Error $_.Exception.Message | |
exit 1 | |
} | |
} | |
} | |
process { | |
foreach ($group in $Groups) { | |
Write-Verbose "Obtaining information for distribution group: '$group'" | |
Write-Verbose "Obtaining information for distribution group: '$group'" | |
try { | |
$reportData.Add($(Get-DistributionGroup -Identity "$group" -ErrorAction Stop | Select-Object -Property *) ) | |
} | |
catch { | |
Write-Warning "Failed to obtain information for $group" | |
Write-Warning $_.Exception.Message | |
} | |
} | |
} | |
end { | |
Write-Verbose "Exporting information to file" | |
$exportSplat = @{ | |
InputObject = $($reportData) | |
Path = "$($env:USERPROFILE)\Downloads\DgInfoExport_$($group.replace('@','_').replace('.','_')).csv" | |
NoTypeInformation = $true | |
Force = $true | |
} | |
Write-Debug "Exporting data to '$($exportSplat.Path)'" | |
try { | |
Export-Csv @exportSplat | |
} | |
catch { | |
Write-Warning "Failed to export output file" | |
Write-Debug "Failed to write file to '$($exportSplat.Path)'" | |
Write-Debug $_.Exception.Message | |
} | |
Write-Debug "Disconnecting from Exchange Online" | |
Disconnect-ExchangeOnline -Confirm:$false | |
if ($RevealInFileSystem) { | |
if ($IsWindows -eq $true) { | |
Write-Verbose "Revealing output file in the file system" | |
Start-Process -Path "explorer.exe" -ArgumentList "$($env:USERPROFILE)\Downloads" | |
} | |
else { | |
Write-Warning "Opening output file is currently only supported on Windows platforms." | |
Write-Debug "Opening output path requires 'explorer.exe'" | |
} | |
} | |
if ($OpenOutputFile) { | |
Write-Verbose "Opening output file in default application for .csv files" | |
Write-Debug "Opening '$($explortSplat.Path)'" | |
Invoke-Item -Path "$($exportSplat.Path)" | |
} | |
} | |
} | |
# Optionally, uncommend the below lines and update the `-Groups` parameter to run the function immediately without needing to dot-source the function file. | |
# Export-DistributionGroupInfo -Groups @( | |
# '[email protected]', | |
# '[email protected]', | |
# '[email protected]' | |
# ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment