Microsoft Teams – script to generate teams owners ,visibility type, owners count, members count and archive status

Microsoft Teams utilization have phenomenally increased with the current COVID situation where almost everyone of us are working from the home. Microsoft Teams being one of the top collaboration software helping all of us to stay better connected during this time.

Most of the organizations doesn’t restrict the Team creation from Microsoft Teams because this factor is heavily influenced on better adoption rate of the Teams communication platform. The below script can be used to run in task scheduler or in Azure Function on a monthly basis for reviewing the Teams Created in last 30 days especially to see the Teams that have been archived and to see the fashion of teams created private or public by the users.

Below is the sample output of the script which will provide us the below details.

Teams Raw – This has the total number of teams with below information.

Teams Single Member – This is to identify teams with just single member

Teams Single Owner – This will report Teams with single Owner

Teams wit no Owner – This will report Teams with no Owner

So the output will have above information on all 4 exports.
############################################################################################################################################
# Description   :- Powershell Script To extract Teams Name,Owner,backup owner,owner count,member count,Group Type and Archive Status.
# Created Date  :- 10-Oct-2020
# Created By    :- Sathish Veerapandian
# Version       :- 0.2
# Imp Notes     :- Please ensure you have folder C:\Scripts and clear the output files generated every time when you run the script again.
# Info          :- If you want to send reports as email please uncommentlast line and use the from/to address with SMTP Server
############################################################################################################################################


$Path="C:\scripts\TeamsReport.csv"
try
{
    Connect-MicrosoftTeams -erroraction SilentlyContinue
    [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
}
catch [System.Exception]
{
write-host "Make sure Teams PowerShell Module is Installed"
return
}

$Header = @"


TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}<br />
TH {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: #48D1CC;}<br />
TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;background-color: #F0FFFF}<br />
"@

$Count = 0
Get-Team | foreach {
$TeamName = $null ; $TeamName = $_.DisplayName
$GroupId = $null ; $GroupId = $_.GroupId
$Visibility = $null ; $Visibility = $_.Visibility
$EmailAlias = $null; $EmailAlias = $_.MailNickName
$Archived = $null; $Archived = $_.Archived

$Count++
Write-Progress -Activity "`n Processed Teams count: $Count "`n"  Currently Processing: $TeamName"

$TeamMembersCount = $null ; $TeamMembersCount = (Get-TeamUser -GroupId $GroupId).count
$TeamOwners = $null ; $TeamOwners = Get-TeamUser -GroupId $GroupId -role Owner
$TeamMembers = $null ; $TeamMembers = Get-TeamUser -GroupId $GroupId -role Member
$TeamOwnersCount = $null ; $TeamOwnersCount = ($TeamOwners).count
$Owner1 = ""
$Owner2 = ""
$member= ""

If ($TeamOwnersCount -eq 1) { $Owner1 = $TeamOwners[0].User}
Elseif ($TeamOwnersCount -ge 2) { $Owner1 = $TeamOwners[0].User; $Owner2 = $TeamOwners[1].User}
If ($TeamOwnersCount -eq 0) {$member = $TeamMembers[0].User}  
If ($TeamOwnersCount -ge 1) {$member = $TeamMembers[0].User}

$Output = [PSCustomObject]@{
    TeamName = $Teamname
    Owner1 = $Owner1
    Owner2 = $Owner2
    TeamOwnersCount = $TeamOwnersCount
    TeamMembersCount = $TeamMembersCount
    TeamEmailAlias = $EmailAlias
    TeamVisibility = $Visibility
    TeamArchiveStatus = $Archived
}
$Output | select * | Export-Csv $Path -NoTypeInformation -Append
$Data = Import-CSV "C:\scripts\TeamsReport.csv"
$Data| Where-Object {$_.TeamOwnersCount -eq 1} | select Team,Owner1,TeamOwnersCount,TeamMembersCount,Member,TeamEmailAlias,TeamVisibility | Export-Csv C:\Temp\TeamsSingleOwner.csv -NoTypeInformation -Append
$Data| Where-Object {$_.TeamOwnersCount -eq 0} | select Team,Owner1,TeamOwnersCount,TeamMembersCount,Member,TeamEmailAlias,TeamVisibility | Export-Csv C:\Temp\TeamswithNoOwner.csv -NoTypeInformation -Append
$Data| Where-Object {$_.TeamMembersCount -eq 1} | select Team,Owner1,TeamOwnersCount,TeamMembersCount,Member,TeamEmailAlias,TeamVisibility | Export-Csv C:\Temp\TeamsSingleMember.csv -NoTypeInformation -Append
$data | ConvertTo-Html -Head $Header | Out-File -FilePath C:\Temp\TeamsReport.html
# Send the exported html as email for evaluation
#Send-MailMessage -From senderemailID -To recipientemailid -Attachments "C:\Scripts\TeamsReport.html" -BodyAsHtml -SmtpServer mentionsmtpserver -Subject TeamsGroupReport
}

Thanks & Regards

Sathish Veerapandian

2 thoughts on “Microsoft Teams – script to generate teams owners ,visibility type, owners count, members count and archive status

  1. Gijs October 10, 2020 at 7:40 pm Reply

    Hi nice script, do not understand line 28:

    $Count++ this trows me a error unknow

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.