Entra / Microsoft 365 · Groups
Rename Microsoft 365 groups for naming policy
A script to rename Microsoft 365 Groups that don't comply with the tenant naming policy.
Connect & set up
Run these once per session. All scopes are read-only unless the script makes changes.
# Review required modules and connection steps before running.# Connect to Microsoft Graph or Exchange Online as needed for this script.
Run it
The main script. Copy it, or download the .ps1 and run it from your console.
Write-Host "Checking that prerequisite PowerShell modules are loaded..."Try { $OrgName = (Get-OrganizationConfig).Name }Catch {Write-Host "Your PowerShell session is not connected to Exchange Online."Write-Host "Please connect to Exchange Online using an administrative account and retry."Break }$AzureADCheck = Get-Module -Name AzureADPreviewIf ($AzureADCheck -eq $Null) {Write-Host "Your PowerShell session is not connected to Azure Active Directory (Preview)."Write-Host "Please connect to Azure Active Directory using an administrative account and retry."; Break }# Get organization naming policy setting$Policy = Get-AzureADDirectorySetting | ? {$_.DisplayName -eq "Group.Unified"}$NamingPolicy = $Policy["PrefixSuffixNamingRequirement"]If (!($NamingPolicy)){ Write-Host "No naming policy defined..."EXIT }Else{ Write-Host "Groups naming policy is" $NamingPolicy }# Check if a prefix or a suffix is used to name groups$Suffix = $False; $Prefix = $FalseIf ($NamingPolicy.Substring(0,11) -eq "[GroupName]") { # Suffix is used, not a prefix$Suffix = $NamingPolicy.Split("]")[1]$GroupsNameMatch = "*" + $Suffix + "*" }Else { #Assume a prefix$Prefix = $NamingPolicy.SubString(0,($NamingPolicy).IndexOf("[GroupName"))$GroupsNameMatch = "*" + $Prefix + "*" }# Find Microsoft 365 Groups that don't match the naming policy$Groups = (Get-UnifiedGroup | ? {$_.DisplayName -NotLike $GroupsNameMatch})If ($Groups.Count -gt 0){ $Prompt = "You have " + $Groups.Count + " groups to update. Proceed? [Y/N]"$Answer = Read-host -Prompt $PromptIf ($Answer.ToUpper() -ne "Y"){Write-Host "Exiting..."EXIT }}# Update GroupsWrite-Host "Updating Groups with new display names..."ForEach ($G in $Groups) {If ($Prefix -ne $False) { # Update with a new prefix$NewDisplayName = $Prefix + $G.DisplayNameWrite-Host $G.DisplayName "updated to" $NewDisplayNameSet-UnifiedGroup -Identity $G.DistinguishedName -DisplayName $NewDisplayName} #End IfElseif ($Suffix -ne $False) {$NewDisplayName = $G.DisplayName + $SuffixWrite-Host $G.DisplayName "updated to" $NewDisplayNameSet-UnifiedGroup -Identity $G.DistinguishedName -DisplayName $NewDisplayName} #End Elseif} #End ForEach
Attribution
Author
Office365itpros