Microsoft 365 governance

PowerShell script: What are my inactive teams

A PowerShell script for Microsoft 365 which finds you inactive teams and list them with export option for easier analysis.

If you are trying to find a list of all your inactive teams then this PowerShell script is just what you need.

The script’s purpose is to find all the teams that have been inactive for a specific time period – 30 days by default.  

The script gets all the teams, and for each team, it checks Teams chat activity. If you want to check file activity in a team-related SharePoint site and Outlook conversations as well try our Syskit Point – free trial.

This PowerShell script will output the teams that have been inactive for a specific time period (30 days by default). If the time period needs to be changed, you can use the -InactivityPeriod parameter, which accepts the number of days.  

Example: Get-InactiveTeams.ps1 -InactivityPeriod 120  

By default, the script outputs the result only to the screen. There are optional flags available to export the result to CSV  

-ExportCSV (if set, this will automatically export the result to a CSV file. If you do not set an export path, it will export the file to %temp% folder)  

Example: Get-InactiveTeams.ps1 -InactivityPeriod 60 -ExportCSV  

-ExportFilePath  

Example: Get-InactiveTeams.ps1 -ExportCSV -ExportFilePath “C:Tempreport.csv” 

PowerShell Script code:

# Set script parameters
[CmdletBinding()]
param (
    [int]$InactivityPeriod = 30,
    [switch]$ExportCSV,
    [string]$ExportFilePath = "$env:TEMP\$(Get-Date -Format "yyyy_MM_dd")InactiveTeams.csv"
)

# Check or install Microsoft Graph Reports module
if ($null -eq (Get-Module -ListAvailable Microsoft.Graph.Reports)) {
    try {
        Write-Output "Microsoft Graph Reports module not found. Trying to install"
        [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
        Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force | Out-Null
        Install-Module Microsoft.Graph.Reports -Force | Out-Null
        Write-Output "Microsoft Graph Reports module installed"
    }
    catch {
        Write-Output "Unable to install Microsoft Graph Reports module. $($Error[0].Exception.Message)"
        Return
    }
}

# Connect to M365
try {
    Write-Output "Connecting Microsoft Graph"
    Select-MgProfile -Name "beta"
    Connect-MgGraph -Scopes Reports.Read.All | Out-Null
}
catch {
    Write-Output "Unable to connect to M365. $($Error[0].Exception.Message)"
    Return
}

# Get activity report
try {
    Get-MgReportTeamActivityDetail -Period 'ALL' -OutFile "$env:Temp\teamsactivityreport.json"
}
catch {
    Write-Output "Unable to get activity report. $($Error[0].Exception.Message)"
    Return
}

# Read the report for filtering
$AllTeamActivity = (Get-Content -Raw "$env:Temp\teamsactivityreport.json" | ConvertFrom-Json).Value 

# Check each team and build a report 
$FinalReport = @()
foreach ($SingleTeamActivity in $AllTeamActivity) {
    if ($SingleTeamActivity.lastActivityDate) {
        $lastActivityDate = Get-Date -Date $SingleTeamActivity.lastActivityDate
        if ($lastActivityDate -le $((Get-Date).AddDays(-$InactivityPeriod))) {
            $TeamActivityObject = [PSCustomObject]@{
                TeamId           = $SingleTeamActivity.teamId
                TeamName         = $SingleTeamActivity.teamName
                LastActivityDate = $SingleTeamActivity.lastActivityDate
            }
            $FinalReport += $TeamActivityObject
        }
    }
    else {
        $TeamActivityObject = [PSCustomObject]@{
            TeamId           = $SingleTeamActivity.teamId
            TeamName         = $SingleTeamActivity.teamName
            LastActivityDate = "No activity found"
        }
        $FinalReport += $TeamActivityObject
    }
}

# Print result to the screen
Write-Output $FinalReport | Format-Table

# Export result to CSV file if needed
if ($ExportCSV) {
    $FinalReport | Export-Csv -Path $ExportFilePath -NoTypeInformation
    Write-Output "Report saved to $ExportFilePath"
}

# Stop before closing powershell window
Read-Host "Script completed. Press 'Enter' to finish"

Get a list of inactive teams quickly with Syskit Point

Syskit Point a Microsoft 365 governance and management platform, can get you a list of inactive teams by checking team chat activity, team-related SharePoint site activity, and Outlook conversations.

Syskit Point can generate various reports in real time. You can easily access them online and share them with your colleagues or managers with a simple link.

Additionally, you can schedule reports, export them in excel or PDF, and perform management actions directly from a report. Easily check Project Server sites, security and database information, operational policies, time and task management settings, the projects list, and project permissions.

Related Posts