December 13, 2024

SamTech 365

PowerPlatform, Power Apps, Power Automate, PVA, SharePoint, C#, .Net, SQL, Azure News, Tips ….etc

Export all Microsoft Teams Members and Owners to a CSV File using PowerShell

PowerShell

In this script, we will look at exporting all Microsoft Teams’ Owners and Members to a CSV file.

This extract might help to communicate with all the owners, in my previous example, we had to send a notification to all the owners, telling them that the teams will be switched from Public to Private.

 

List all the teams’ owners and memebrs

Requirements

The following PowerShell script requires the following:

  1. Microsoft Teams module for powershell  available from:
    1. Install the Microsoft Teams module via PowerShell Gallery(recommended option).
    2. Install the Microsoft Teams module via Manual Download.
  2. Having Teams or Tenant admin rights.

Once these two requirements were in place, time for the action:

 

The script

$Result = ""  
$Results = @() 
$Path = "./All Teams Members and Owner Report_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv"
Write-Host Exporting all Teams members and owners report...
$Count = 0
Get-Team | foreach {
    $TeamName = $_.DisplayName
    Write-Progress -Activity "`n     Processed Teams count: $Count "`n"  Currently Processing: $TeamName"
    $Count++
    $GroupId = $_.GroupId
    Get-TeamUser -GroupId $GroupId | foreach {
        $Name = $_.Name
        $MemberMail = $_.User
        $Role = $_.Role
        $Result = @{'Teams Name' = $TeamName; 'Member Name' = $Name; 'Member Mail' = $MemberMail; 'Role' = $Role }
        $Results = New-Object psobject -Property $Result
        $Results | select 'Teams Name', 'Member Name', 'Member Mail', 'Role' | Export-Csv $Path -NoTypeInformation -Append
    }
}
Write-Progress -Activity "`n     Processed Teams count: $Count "`n"  Currently Processing: $TeamName" -Completed
if ((Test-Path -Path $Path) -eq "True") {
    Write-Host `nReport available in $Path -ForegroundColor Green
}