24 February 2021

Change domain for microsoft 365 / azure users from powershell

modify TLS settings if you get errors running commands below
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12


Install-Module -Name MSOnline
(from https://www.powershellgallery.com )

 

Connect-MsolService

$AllMsolUsers = Get-MsolUser -All
$AllToModify = $AllMsolUsers | Where-Object -FilterScript { ($PSItem.Licenses.AccountSkuId -eq 'o365:STANDARDPACK') -and ($PSItem.UserType -eq 'Member') -and $PSItem.IsLicensed}

foreach($User in $AllToModify){

    if($User.UserPrincipalName.Contains('@o365.onmicrosoft.com')){

        $NewPrincipal = $User.UserPrincipalName.Replace('@o365.onmicrosoft.com','@domain.com')

        Set-MsolUserPrincipalName -NewUserPrincipalName $NewPrincipal -UserPrincipalName $User.UserPrincipalName

    }

}


No comments:

Post a Comment