Automate Renewal of Expiring M365 Groups Using PowerShell
Introduction
Managing the lifecycle of Microsoft 365 Groups is important to prevent accidental deletion of M365 Groups. It is a good practice to set lifecycle expiration policy to control sprawl. However that means that the group will get automatically deleted after they expire. The Teams/M365 groups owners will get email notifications to renew within a certain timeframe , however if the owners missed the renewal notifications for different reasons, it may lead to accidental data loss. The script can help identify M365 groups nearing expiration to renew them.
This script will renew expiring M365 groups to ensure they remain active and accessible until the m365 group owners review them for deletion.
Prerequisites
Before you begin, ensure you have the following:
- PnP PowerShell module installed.
- Appropriate permissions to access and manage Microsoft 365 Groups.
PowerShell Script
Below is a PowerShell script that identifies expiring M365 Groups and renews them. The script also logs the renewed groups’ details into a CSV file for auditing purposes.
param (
[Parameter(Mandatory = $true)]
[string] $domain
)
$adminSiteURL = "https://$domain-Admin.SharePoint.com"
$dateTime = "_{0:MM_dd_yy}_{0:HH_mm_ss}" -f (Get-Date)
$invocation = (Get-Variable MyInvocation).Value
$directorypath = Split-Path $invocation.MyCommand.Path
$fileName = "m365_group_expire_reset" + $dateTime + ".csv"
$outputPath = $directorypath + "\"+ $fileName
if (-not (Test-Path $outputPath)) {
New-Item -ItemType File -Path $outputPath
}
Connect-PnPOnline -Url $adminSiteURL -Interactive -WarningAction SilentlyContinue
Get-PnPMicrosoft365ExpiringGroup | ForEach-Object {
$group = $_
Reset-PnPMicrosoft365GroupExpiration -Identity $group.Id
$group = Get-PnPMicrosoft365Group -Identity $group.Id
$group | Select-Object id, RenewedDateTime,DisplayName|Export-Csv -Path $outputPath -NoTypeInformation -Append
}
Example Output
The CSV file will contain columns for id, RenewedDateTime, and DisplayName, which will look like: Output
id,RenewedDateTime,DisplayName
<group_id_1>,2024-10-25T08:25:29Z,Group One
<group_id_2>,2024-10-25T08:25:29Z,Group Two