Find Inactive Teams in Microsoft 365 with PowerShell

Listen to this Post

Microsoft Teams is the go-to collaboration hub for team projects. Every new project gets its own space in Microsoft Teams. But once the project ends, those teams are often left behind and eventually forgotten.

A few inactive teams donโ€™t seem like a big deal. But as they pile up, managing Microsoft Teams securely becomes harder. The problem? Microsoft 365 doesnโ€™t offer a direct solution for tracking inactive teams. Finding the last activity date of teams and figuring out inactive ones are both hassle and time-consuming.โณ

That’s where this PowerShell script helps! Identify all inactive teams over a specific period and take swift action to keep your environment clutter-free.

๐Ÿ‘‰Download the script from: https://lnkd.in/gu-Xfq4e

PowerShell Script Example:


<h1>Connect to Microsoft Teams</h1>

Connect-MicrosoftTeams

<h1>Define the date range for inactivity (e.g., 90 days)</h1>

$inactiveDate = (Get-Date).AddDays(-90)

<h1>Get all teams and filter inactive ones</h1>

$teams = Get-Team
$inactiveTeams = $teams | ForEach-Object {
$team = $_
$lastActivity = Get-TeamChannel -GroupId $team.GroupId | Get-TeamChannelMessage -Top 1 | Select-Object -ExpandProperty CreatedDateTime
if ($lastActivity -lt $inactiveDate) {
[PSCustomObject]@{
TeamName = $team.DisplayName
LastActivity = $lastActivity
}
}
}

<h1>Export inactive teams to CSV</h1>

$inactiveTeams | Export-Csv -Path "InactiveTeams.csv" -NoTypeInformation

<h1>Disconnect from Microsoft Teams</h1>

Disconnect-MicrosoftTeams

What Undercode Say:

Managing inactive teams in Microsoft 365 is crucial for maintaining a clean and secure environment. The provided PowerShell script automates the process of identifying teams that have not been active for a specified period, such as 90 days. This script connects to Microsoft Teams, retrieves all teams, and filters out those with no recent activity. The results are then exported to a CSV file for further review.

In addition to the script, here are some related commands and practices to enhance your IT operations:

1. Check User Activity in Microsoft 365:

Get-MsolUser -All | Select-Object UserPrincipalName, LastLogonTime

2. List All Microsoft 365 Groups:

Get-UnifiedGroup | Select-Object DisplayName, WhenCreated

3. Delete Inactive Teams:

$inactiveTeams | ForEach-Object {
Remove-Team -GroupId $_.GroupId
}

4. Monitor Microsoft 365 Service Health:

Get-ServiceHealth -ServiceName "Microsoft Teams"

5. Audit Logs for Teams:

Search-UnifiedAuditLog -RecordType MicrosoftTeams -StartDate (Get-Date).AddDays(-30) -EndDate (Get-Date)

6. Check License Usage:

Get-MsolAccountSku | Select-Object SkuPartNumber, ActiveUnits, ConsumedUnits

7. Automate Reports with Scheduled Tasks:

Register-ScheduledTask -Action (New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-File C:\Scripts\InactiveTeams.ps1") -Trigger (New-ScheduledTaskTrigger -Daily -At 3am)

8. Backup Teams Data:

Start-Process -FilePath "C:\Path\To\BackupTool.exe" -ArgumentList "/backup /teamid:12345"

9. Check Teams Meeting Activity:

Get-CsMeeting | Select-Object Organizer, StartTime, EndTime

10. Monitor Teams Storage Usage:

Get-TeamChannel -GroupId <TeamId> | Get-TeamChannelFile -Top 100 | Select-Object Name, Size

By implementing these commands and scripts, you can ensure that your Microsoft 365 environment remains efficient and secure. Regularly monitoring and cleaning up inactive teams not only improves performance but also reduces potential security risks. For more advanced automation, consider integrating these scripts with Azure Automation or Logic Apps.

For further reading, visit:

This approach ensures that your IT operations remain streamlined and proactive, reducing clutter and enhancing security in your Microsoft 365 environment.

References:

Hackers Feeds, Undercode AIFeatured Image