Home

PowerShell script to export Microsoft Teams Usage in CSV format used to Audit an Office 365 Tenant

Highlighted
Fabrice Romelard
MVP

After the previous scripts published to audit an Office 365 Tenant:

 

This script is now dedicated to Microsoft Teams case. 

 

Import-Module MSOnline
Import-Module MicrosoftTeams

[string]$ReportPath = ".\Reports\"
[string]$username = "Admin@yourtenant.onmicrosoft.com"
[string]$PwdTXTPath = "C:\SECUREDPWD\ExportedPWD-$($username).txt"
$secureStringPwd = ConvertTo-SecureString -string (Get-Content $PwdTXTPath)
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secureStringPwd

$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $cred -Authentication Basic -AllowRedirection
Import-PSSession $session

#Connect-MicrosoftTeams -Credential $cred
Connect-MicrosoftTeams 

[int]$i = 1;
$data = @()
[string]$ListOfChannelNames = ""
[string]$TeamOwnersEmails = ""
[string]$TeamMembersEmails = ""
[string]$TeamGuestsEmails = ""

$ListOfTeams = Get-Team  -user $username #Test command to focus
#$ListOfTeams = Get-Team

Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor Magenta


foreach($MyTeams in $ListOfTeams)
{
	Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor green

	$TeamUnifiedGroup = Get-UnifiedGroup -Identity $MyTeams.GroupId
	Write-Host "    ===-> Teams GroupID:", $MyTeams.GroupId, "- Name:", $MyTeams.DisplayName, "- Description:", $MyTeams.Description  -ForegroundColor Yellow
	Write-Host "    ===-> GroupEmail:", $TeamUnifiedGroup.PrimarySmtpAddress

	# Current Teams Configuration implemented
	Write-Host "    ===-> Get-TeamMemberSettings:" -ForegroundColor Yellow
    $TeamMemberSettings = Get-TeamMemberSettings -GroupId $MyTeams.GroupId
	Write-Host "          ====> ", $TeamMemberSettings.AllowCreateUpdateChannels

    Write-Host "    ===-> Get-TeamMessagingSettings:" -ForegroundColor Yellow
    $TeamMessagingSettings = Get-TeamMessagingSettings -GroupId $MyTeams.GroupId
	Write-Host "          ====> ", $TeamMessagingSettings.AllowUserEditMessages

    Write-Host "    ===-> Get-TeamGuestSettings:" -ForegroundColor Yellow
    $TeamGuestSettings = Get-TeamGuestSettings -GroupId $MyTeams.GroupId
	Write-Host "          ====> ", $TeamGuestSettings.AllowCreateUpdateChannels

    Write-Host "    ===-> Get-TeamFunSettings:" -ForegroundColor Yellow
    $TeamFunSettings = Get-TeamFunSettings -GroupId $MyTeams.GroupId
	Write-Host "          ====> ", $TeamFunSettings.AllowGiphy

	$ListOfChannelNames = ""
	$TeamOwnersEmails = ""
	$TeamMembersEmails = ""
	$TeamGuestsEmails = ""
    $CurrentListOfChannelList = Get-TeamChannel -GroupId $MyTeams.GroupId
    foreach($MyChannel in $CurrentListOfChannelList)
    {
	    Write-host "      ------------------ " -ForegroundColor green
	    Write-Host "      ===-> Channel Name:", $MyChannel.Id, "- Name:", $MyChannel.DisplayName, "- Description:", $MyChannel.Description  -ForegroundColor Magenta
        $ListOfChannelNames += " -|- "+ $MyChannel.DisplayName
    }

    $TeamUsers = Get-TeamUser -GroupId $MyTeams.GroupId
    Write-Host "    ===-> Get-TeamUser:", $TeamUsers.Count -ForegroundColor Yellow
	if ([array]$MyTeamGuest = $TeamUsers | Where {$_.Role -eq "guest"})
	{
		$TeamGuestsCount = $MyTeamGuest.Count
		$MyTeamGuest.ForEach({ $TeamGuestsEmails += "|"+ $_.User  })
		Write-Host "       ===-> TeamGuestsEmails:", $TeamGuestsEmails -ForegroundColor Magenta
    }
	else
	{
		$TeamGuestsCount = 0
		$TeamGuestsEmails = ""
    }

	if ([array]$MyTeamMembers = $TeamUsers | Where {$_.Role -eq "member"})
	{
		$TeamMembersCount = $MyTeamMembers.Count
		$MyTeamMembers.ForEach({ $TeamMembersEmails += "|"+ $_.User  })
		Write-Host "       ===-> TeamMembersEmails:", $TeamMembersEmails -ForegroundColor Magenta
    }
	else
	{
		$TeamMembersCount = 0
		$TeamMembersEmails = ""
    }

	if ([array]$MyTeamOwners = $TeamUsers | Where {$_.Role -eq "owner"})
	{
		$TeamOwnersCount = $MyTeamOwners.Count
		$MyTeamOwners.Foreach({ $TeamOwnersEmails += "|"+ $_.User  })
		Write-Host "       ===-> TeamOwnersEmails:", $TeamOwnersEmails -ForegroundColor Magenta
    }
	else
	{
		$TeamOwnersCount = 0
		$TeamOwnersEmails = ""
    }

#Team name	TeamMail	Channels	MembersCount	OwnersCount	GuestsCount	Privacy

	$data += @(
		[pscustomobject]@{
		GroupID = $MyTeams.GroupId
		GroupTeamMail = $TeamUnifiedGroup.PrimarySmtpAddress
		GroupHiddenfromOutlook = $TeamUnifiedGroup.HiddenFromExchangeClientsEnabled
		GroupAccessType = $TeamUnifiedGroup.AccessType
		GroupExternalMemberCount = $TeamUnifiedGroup.GroupExternalMemberCount
		TeamName = $MyTeams.DisplayName
		TeamDescription = $MyTeams.Description
		TeamMemberSettingsAllowCreateUpdateChannels = $TeamMemberSettings.AllowCreateUpdateChannels
		TeamMemberSettingsAllowDeleteChannels = $TeamMemberSettings.AllowDeleteChannels
		TeamMemberSettingsAllowAddRemoveApps = $TeamMemberSettings.AllowAddRemoveApps
		TeamMemberSettingsAllowCreateUpdateRemoveTabs = $TeamMemberSettings.AllowCreateUpdateRemoveTabs
		TeamMemberSettingsAllowCreateUpdateRemoveConnectors = $TeamMemberSettings.AllowCreateUpdateRemoveConnectors
		TeamMessagingSettingsAllowUserEditMessages = $TeamMessagingSettings.AllowUserEditMessages
		TeamMessagingSettingsAllowUserDeleteMessages = $TeamMessagingSettings.AllowUserDeleteMessages
		TeamMessagingSettingsAllowOwnerDeleteMessages = $TeamMessagingSettings.AllowOwnerDeleteMessages
		TeamMessagingSettingsAllowTeamMentions = $TeamMessagingSettings.AllowTeamMentions
		TeamMessagingSettingsAllowChannelMentions = $TeamMessagingSettings.AllowChannelMentions
		TeamGuestSettingsAllowCreateUpdateChannels = $TeamGuestSettings.AllowCreateUpdateChannels
		TeamGuestSettingsAllowDeleteChannels = $TeamGuestSettings.AllowDeleteChannels
		TeamFunSettingsAllowGiphy = $TeamFunSettings.AllowGiphy
		TeamFunSettingsGiphyContentRating = $TeamFunSettings.GiphyContentRating
		TeamFunSettingsAllowStickersAndMemes = $TeamFunSettings.AllowStickersAndMemes
		TeamFunSettingsAllowCustomMemes = $TeamFunSettings.AllowCustomMemes
		TeamChannelsCount = $CurrentListOfChannelList.Count
		TeamChannelsNames = $ListOfChannelNames
		TeamOwnersCount = $TeamOwnersCount
		TeamOwnersEmails = $TeamOwnersEmails
		TeamMembersCount = $TeamMembersCount
		TeamMembersEmails = $TeamMembersEmails
		TeamGuestsCount = $TeamGuestsCount
		TeamGuestsEmails = $TeamGuestsEmails
		}
	)


	Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor green
}

Write-host " -------------------------------------------------------------------------------------------- " -ForegroundColor green

$datestring = (get-date).ToString("yyyyMMdd-hhmm")
$fileName = Join-Path -Path $ReportPath  -ChildPath $("O365-TeamsDetails_"+ $datestring + ".csv")
	
Write-host " -----------------------------------------" -ForegroundColor Green
Write-Host (" >>> writing to file {0}" -f $fileName) -ForegroundColor Green
$data | Export-csv $fileName -NoTypeInformation -enc utf8 
Write-host " -----------------------------------------" -ForegroundColor Green

 

You can adapt that script as you need, based on your own requirements

 

Fabrice Romelard

 

French version:

Source used: