You might have a bunch of MS Teams groups in your organization. And you need to generate a group member report for your manager.
This can be done by Powershell easily rather than manually type/copy them in the web interface.
Different from MS AD group, the teams groups are not synced back to your AD environment. the command we use here is get-unifiedGroup
1. create a session to the O365
This step, we will create a session to MS exchange modue
$Credential = Get-Credential #Create the session $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ ` -Credential $Credential -Authentication Basic -AllowRedirection #Import the session #Import-PSSession $Session -DisableNameChecking -AllowClobber Import-Module (Import-PSSession $Session -AllowClobber) -Global
2. Get the group with keywords and look through them
Before we doing this code, instead of guess them blindly, we can get the name format first,
First, I did a search in the web console,
Then click one of the group name:
Due to the reason that it’s hard to filter by the primary address, we want to find the name format, I will take this to analysis:
get-unifiedgroup -identity "Your_Teams_group_primary_email"
We can find in the Name column, the name is reflecting the primary email address and Azure ID, which has been deleted in the picture.
And the command get-unifiedGroup will be:
#Get All Office 365 Groups with 2021_ in the middle $O365Groups=Get-UnifiedGroup -Verbose -Filter "Name -like '*2021*'"
Then we will use a ForEach loop, and command Get-unifiedGroupLinks to get the member:
ForEach ($Group in $O365Groups) { $CSVPath = "C:\Temp\"+$Group +".csv" 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 }
Note that the CSV file path is named after the group, and will be saved to c:\Temp, so make sure you have a folder called Temp in C drive.
The whole script will be:
#Get Credentials to connect $Credential = Get-Credential #Create the session $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ ` -Credential $Credential -Authentication Basic -AllowRedirection #Import the session #Import-PSSession $Session -DisableNameChecking -AllowClobber Import-Module (Import-PSSession $Session -AllowClobber) -Global #Remove the CSV file if exists #If(Test-Path $CSVPath) { Remove-Item $CSVPath} #Get All Office 365 Groups with 2021_ in the middle #$O365Groups=Get-UnifiedGroup -Identity "[email protected]" $O365Groups=Get-UnifiedGroup -Verbose -Filter "Name -like '*2021_*'" ForEach ($Group in $O365Groups) { $CSVPath = "C:\Temp\"+$Group +".csv" 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 } #Remove the session Remove-PSSession $Session