PowerShell: Get Group list and Members

Get Group List to CSV File:

 

Get-UnifiedGroup | Select DisplayName,Alias, AccessType,ManagedBy,WhenCreated | Export-Csv -Path “C:\Temp\Groups.csv” -NoTypeInformation

 

GET ALL Group Members List to CSV File:

 

$CSVPath = “C:\Temp\GroupMembers.csv”

#Connect to Exchange Online

Connect-ExchangeOnline -ShowBanner:$False

#Remove the CSV file if exists

If(Test-Path $CSVPath) { Remove-Item $CSVPath}

#Get All Office 365 Groups

$O365Groups=Get-UnifiedGroup

ForEach ($Group in $O365Groups)

{

    Write-Host “Group Name:” $Group.DisplayName -ForegroundColor Green

    Get-UnifiedGroupLinks -Identity $Group.Id -LinkType Members | Select DisplayName,PrimarySmtpAddress

 

    #Get Group Members and export to CSV

    Get-UnifiedGroupLinks -Identity $Group.Id -LinkType Members | Select-Object @{Name=”Group Name”;Expression={$Group.DisplayName}},`

         @{Name=”User Name”;Expression={$_.DisplayName}}, PrimarySmtpAddress | Export-CSV $CSVPath -NoTypeInformation -Append

}

#Disconnect Exchange Online

Disconnect-ExchangeOnline -Confirm:$False

Tags:

Comments are closed

Latest Comments

No comments to show.