How to export owners, members and other details of a Microsoft Team based on user ownership status

Problem

You have been asked to provide a list of all Microsoft Teams that a user owns. The list should include the members of each Team, owners and Teams details. You are provided with a long list of users to run this task against.

Solution

An easy way to do this is by developing a PowerShell script. I had a need to provide these details during the course of a Teams to Teams migration activity. In the script below, these are the steps I applied.

  1. Import a list of user’s UPN from a csv file.
  2. Foreach user in the list, identify Teams ID they are a member of.
  3. Foreach identified Team, export all users in the Team, their roles, UPN, the Team ID, DisplayName of the user and the number of owners in the Team.
  4. Foreach Team associated with each of the users exported, verify if the user is an owner of the Team.
  5. If the user is an owner of the Team in question and the number of owners is greater than 1, export the DisplayName of the team, email address of the Team, Description, Visibility, MailNickName of the Team.
  6. If the number of owners in the Team is equal to 1, export the DisplayName of the team, email address of the Team, Description, Visibility, MailNickName of the Team.
  7. Combine both exports into a single list. This gives you a list of all the Teams.
  8. Using this list, verify the number of owners identified in each and generate another list indicating the ones that are owned specifically by the user in question or owned by multiple owners including the user in question. The final list should include the following details:
    • Team ID
    • Role of the user
    • Team DisplayName they below to
    • Team MailNickName
    • User UPN
    • User DisplayName
    • Team EmailAddress
    • Number Of Owners in the Team
  9. Finally, I found a little bug that I could not find a way to fix. This final list returns a blank user detail in the first index. I excluded that using the final if statement.

Full Script:

#You will need a connection to Microsoft Teams and Exchange online

$TeamsExport = @()
$Importfile = Import-csv "C:\Users.csv"
Foreach($DUser in $ImportFile)
{
$TeamID = @()
$TeamID = Get-Team -User $DUser.UPN | select groupID
$Teams = @()
$OwnedGroup = @()
foreach($Team in $TeamID) {
$OwnerCount = $Null
$OwnerCount = ((Get-Teamuser -GroupId $Team.GroupId).role -eq "owner").count
$OwnedGroup = Get-Teamuser -GroupId $Team.GroupId
$Count = @()
$Count = (Get-Teamuser -GroupId $Team.GroupId).count
Do{
$Teams += New-Object PSObject -property @{
Role = $OwnedGroup.Role[$Count]
TeamID = $Team.GroupId
User = $OwnedGroup.User[$Count]
Name = $OwnedGroup.Name[$Count]
UPN = $DUser.UPN
NumberOfOwners = $OwnerCount
}
$Count--
}
while($Count -gt 0)
}
Foreach($Group in $Teams){
If(($Group.Role -eq "Owner") -and ($Group.User -eq $Group.UPN) -and ($Group.NumberOfOwners -gt 1))
{
$GroupValue = $Null
$GroupValue = Get-Team -GroupId $Group.TeamID |select DisplayName,Description,Visibility,MailNickName
$TeamsExport += New-Object PSObject -property @{
GroupId = $Group.TeamID
OwnerCount = $Group.NumberOfOwners
OwnerDisplayName = $Group.Name
OwnerUPN = $Group.UPN
GroupDisplayName = $Groupvalue.DisplayName
Description = $GroupValue.Description
Visibility = $GroupValue.Visibility
MailNickName = $GroupValue.MailNickName
GroupEmailAddress = (Get-Unifiedgroup -Identity $Group.TeamID).primarysmtpaddress
}
}
elseif(($Group.Role -eq "Owner") -and ($Group.User -eq $Group.UPN) -and ($Group.NumberOfOwners -eq 1))
{
$GroupValue = $Null
$GroupValue = Get-Team -GroupId $Group.TeamID |select DisplayName,Description,Visibility,MailNickName
$TeamsExport += New-Object PSObject -property @{
GroupId = $Group.TeamID
OwnerCount = 1
OwnerDisplayName = $Group.Name
OwnerUPN = $Group.UPN
GroupDisplayName = $Groupvalue.DisplayName
Description = $GroupValue.Description
Visibility = $GroupValue.Visibility
MailNickName = $GroupValue.MailNickName
GroupEmailAddress = (Get-UnifiedGroup -Identity $Group.TeamID).primarysmtpaddress
}
}
}
}
$MemberExport = @()
Foreach($Item in $TeamsExport)
{
$GOwnerCount =$Null
$GOwnerCount = ((Get-Teamuser -GroupId $Item.GroupId).role -eq "owner").count
If($GOwnerCount -gt 1)
{
$MemberCount = $Null
$MemberCount = (Get-Teamuser -GroupId $Item.GroupId).count
$Membervalue = $Null
$Membervalue = Get-Teamuser -GroupId $Item.GroupId | select User, Role, Name
$TeamEmailAddress =$Null
$TeamEmailAddress = (Get-UnifiedGroup -Identity $Item.GroupId).primarysmtpaddress
Do{
$MemberExport += New-Object PSObject -property @{
TeamID = $Item.GroupId
Role = $Membervalue.Role[$MemberCount]
TeamDisplayName = $Item.GroupDisplayName
TeamMailNickName = $Item.MailNickName
MemberUPN = $Membervalue.User[$MemberCount]
MemberDisplayName = $Membervalue.Name[$MemberCount]
TeamEmailAddress = $TeamEmailAddress
NumberOfOwners = $GOwnerCount
}
$MemberCount--
}
while($MemberCount -gt 0)
}
elseif($GOwnerCount -eq 1)
{
$MemberCount = $Null
$MemberCount = (Get-Teamuser -GroupId $Item.GroupId).count
$Membervalue = $Null
$Membervalue = Get-Teamuser -GroupId $Item.GroupId | select User, Role, Name
$TeamEmailAddress =$Null
$TeamEmailAddress = (Get-UnifiedGroup -Identity $Item.GroupId).primarysmtpaddress
Do{
$MemberExport += New-Object PSObject -property @{
TeamID = $Item.GroupId
Role = $Membervalue.Role[$MemberCount]
TeamDisplayName = $Item.GroupDisplayName
TeamMailNickName = $Item.MailNickName
MemberUPN = $Membervalue.User[$MemberCount]
MemberDisplayName = $Membervalue.Name[$MemberCount]
TeamEmailAddress = $TeamEmailAddress
NumberOfOwners = 1
}
$MemberCount--
}
while($MemberCount -gt 0)
}
}
Write-host "Teams" -ForegroundColor Yellow
$TeamsExport
$MemberExportB = @()
Foreach($M in $MemberExport)
{If($M.MemberUPN -ne $Null)
{
$MemberExportB += New-Object PSObject -property @{
TeamID = $M.TeamID
Role = $M.Role
TeamDisplayName = $M.TeamDisplayName
TeamMailNickName = $M.TeamMailNickName
MemberUPN = $M.MemberUPN
MemberDisplayName = $M.MemberDisplayName
TeamEmailAddress = $M.TeamEmailAddress
NumberOfOwners = $M.NumberOfOwners}
}
}
Write-host "Members" -ForegroundColor Yellow
$MemberExportB

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s