Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save matsimon/047e703af95e4158356142e817157083 to your computer and use it in GitHub Desktop.

Select an option

Save matsimon/047e703af95e4158356142e817157083 to your computer and use it in GitHub Desktop.
A Powershell-Script that changes existing Teams/Unified Groups primary Mail address since Exchange Online Adress policies only apply to new groups
Connect-ExchangeOnline
"DisplayName,Modified,Primary,Leftovers" | Out-File -FilePath modifications.txt
$UnifiedGroups = Get-UnifiedGroup -ResultSize unlimited
foreach ($Group in $UnifiedGroups) {
$modified = $false
foreach ($address in $Group.EmailAddresses){
if ($address -match "@oldprimarydomain.tld"){
# primaryAddress has not "SMTP:"
$updatedAdress = $address -replace "@oldprimarydomain.tld","@groups.oldprimarydomain.tld"
$primaryAddress = $updatedAdress -replace "SMTP:",""
# Define new primary address, then remove old one
$modified = $true
Set-UnifiedGroup -Identity $Group.Name -PrimarySmtpAddress $primaryAddress
Set-UnifiedGroup -Identity $Group.Name -EmailAddresses @{remove=$address}
}
}
$updatedGroup = Get-UnifiedGroup -Identity $Group.Name
$leftovers = $false
foreach ($address in $updatedGroup.EmailAddresses){
if ($address -match "@oldprimarydomain.tld"){
$leftovers = $true
}
}
$a = -join($updatedGroup.DisplayName,";",$modified,";",$updatedGroup.PrimarySmtpAddress,";",$leftovers)
Write-Host $a
$a | Out-File -Append -FilePath modifications.txt
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment