I'm looking for a way to export a list of distribution groups along with the members of each group via PowerShell on Office 365.
I've found scripts that export a list of users and the groups they belong too but not the other way round?
I got close with this:
$saveto = "C:\\listmembers.txt"
Get-DistributionGroup | sort name | ForEach-Object {
"`r`n$($_.Name)`r`n=============" | Add-Content $saveto
Get-DistributionGroupMember $_ | sort Name | ForEach-Object {
If($_.RecipientType -eq "UserMailbox")
{
$_.Name + " (" + $_.PrimarySMTPAddress + ")" | Add-Content $saveto
}
}
}
But it produces a text file with a list of all Distribution groups and is divided nicely by the "====" but no members are displayed. I receive the below error on each group it finds:
Cannot process argument transformation on parameter 'Identity'. Cannot convert value "GROUP NAME" to type "Microsoft.Exchange.Configuration.Tasks.DistributionGroupMemberIdParameter". Error:
"Cannot convert hashtable to an object of the following type: Microsoft.Exchange.Configuration.Tasks.DistributionGroupMemberIdParameter. Hashtable-to-Object conversion is not supported in rest
ricted language mode or a Data section."
+ CategoryInfo : InvalidData: (:) [Get-DistributionGroupMember], ParameterBindin...mationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-DistributionGroupMember
Best Answer
I tried the response from Vaibhav but it didn't work straight away. I resolved my issue with the following code.
Running this Output the group DisplayName followed by the User DisplayName in a list underneath.