Export all members of all distribution groups

Connect-MsolService
$a = @()
$Groups = Get-MsolGroup -All
foreach ($Group in $Groups) {
    $Members = Get-MsolGroupMember -GroupObjectId $($Group.ObjectId) -All
    foreach ($Member in $Members) {
        $item = New-Object PSObject
        $item | Add-Member -type NoteProperty -Name 'Group' -Value $(($Group).DisplayName)
        $item | Add-Member -type NoteProperty -Name 'GroupType' -Value $(($Group).GroupType)
        $item | Add-Member -type NoteProperty -Name 'User' -Value $(($Member).DisplayName)
        $item | Add-Member -type NoteProperty -Name 'UserEmail' -Value $(($Member).EmailAddress)
        $a += $item
    }
}
$a | Export-CSV -Path "C:\Some\Path.csv"

Leave a Reply