Getting a List of Microsoft Teams and Their Members using PowerShell
If you're an Office 365 administrator, you may need to retrieve a list of all the Microsoft Teams in your organization and their members. This information can be helpful for tasks like auditing, reporting, and troubleshooting. Fortunately, you can use PowerShell to quickly retrieve this information and save it to a CSV file for easy viewing and sharing.
Here's a PowerShell script that will retrieve all the Microsoft Teams in your organization and their members and save the information to a CSV file:
$creds = Get-Credential
Connect-MicrosoftTeams -Credential $creds
$teams = Get-Team
$output = @()
foreach ($team in $teams) {
$teamName = $team.DisplayName
$owners = Get-TeamUser -GroupId $team.GroupId -Role Owner
$members = Get-TeamUser -GroupId $team.GroupId -Role Member
foreach ($owner in $owners) {
$output += [PSCustomObject]@{
TeamName = $teamName
Role = "Owner"
MemberName = $owner.User.displayName
}
}
foreach ($member in $members) {
$output += [PSCustomObject]@{
TeamName = $teamName
Role = "Member"
MemberName = $member.User.displayName
}
}
}
$output | Export-Csv -Path "TeamsAndMembers.csv" -NoTypeInformation
Let's break down how this script works:
The script prompts the user for their credentials using the 'Get-Credential' cmdlet.
The script then connects to Microsoft Teams using the 'Connect-MicrosoftTeams' cmdlet.
The script retrieves all the Microsoft Teams in the organization using the 'Get-Team' cmdlet.
For each team, the script retrieves the team owners and members using the Get-TeamUser cmdlet with the '-Role' parameter set to either "Owner" or "Member".
The script creates an array of 'PSCustomObjects' to store the team name, role (owner or member), and member name for each member and owner in each team.
The script then exports the array of 'PSCustomObjects' to a CSV file using the 'Export-Csv' cmdlet.
The resulting CSV file will have three columns: "TeamName", "Role", and "MemberName". Each row will represent a team member or owner, with the "Role" column indicating whether they are an owner or a member of the team.
You can modify the output file path and name by modifying the -Path parameter of the Export-Csv cmdlet.
In summary, PowerShell provides an easy and efficient way to retrieve a list of all the Microsoft Teams in your organization and their members. By using the script above, you can quickly retrieve this information and save it to a CSV file for easy viewing and sharing.
Thanks for reading me .
Comments