PowerShell/Office 365 - Get the distribution groups membership
I was recently looking at Office365/Exchange Online to retrieve Distribution Group membership. This is pretty simple using something like: Get-DistributionGroupMember -Identity "Marketing"
Not that prior to perforce the following command you need to be already connected to Office365, see this post.
Retrieving all members of each Distribution Group
Now if I want to retrieve all the Distribution group members, it’s bit trickier you’ll see. First I started by listing the groups members. My PowerShell instinct made me type Get-DistributionGroup | Get-DistributionGroupMember
but it did not work, see below:
As the error message is stating that this Cmdlet does not accept input :-/
By using Get-Help
on Get-DistributionGroupMember
, we can check the parameters accepting Input:
(Get-Help Get-DistributionGroupMember).parameters.parameter|
Select-Object name,parametervalue,pipelineinput
The Identity parameter is not accepting the value by property name, so we have to specify Identity
before we send the output to Get-DistributonGroupMember
(Get-DistributionGroup -ResultSize 5).identity|
Get-DistributionGroupMember
The above output does the job but we do not know which group they belong to. You can include the Distribution group name in the output using the following:
(Get-DistributionGroup).identity | ForEach-Object{
$DistributionGroupName = $_
Get-DistributionGroupMember -Identity $_ | ForEach-Object{
[PSCustomObject]@{
DistributionGroup = $DistributionGroupName
MemberName = $_.Name
#Other recipient properties here
}
}
}
Retrieving a single Distribution Group membership
Pretty similar to the one above, just specify the name of the Distribution Group
(Get-DistributionGroup -Identity 'Sales').identity | ForEach-Object{
$DistributionGroupName = $_
Get-DistributionGroupMember -Identity $_ | ForEach-Object{
[PSCustomObject]@{
DistributionGroup = $DistributionGroupName
MemberName = $_.Name
#Other recipient properties here
}
}
}
Retrieving Distribution Groups membership with a specific name pattern
You can also specify the pattern of distribution groups you are looking for.
Here it will return all the Distribution Group starting by MTL
.
(Get-DistributionGroup -Identity 'MTL*').identity | ForEach-Object{
$DistributionGroupName = $_
Get-DistributionGroupMember -Identity $_ | ForEach-Object{
[PSCustomObject]@{
DistributionGroup = $DistributionGroupName
MemberName = $_.Name
#Other recipientproperties here
}
}
}
Leave a comment