#Office365的租户经常会有购买了新的域名的情况,即使公司内部有多个域名,每个账户能使用的登录域名也只有一个,所以经常会需要更改域名的情况,可以调用PowerShell的replace()方法来实现一个简易的脚本。
#Office365 tenants often buy a new domain name. Even if there are multiple domain names in the company, each account can use only one login domain name. Therefore, it is often necessary to change the domain name. You can call PowerShell's replace( ) Method to implement a simple script.

#Script starts
Connect-msolservice

#$OldDomain, $NewDomainm, $Users needs to be changed
#$OldDomain, $NewDomainm, $Users 三个变量需要自定义更改

$OldDomain = "gesinfotek.com"
$NewDomain = "geservs.com"

$users = get-content "C:/users.txt"

Foreach($user in $users)
{
    $NewUPN = ($user.userprincipalname).replace($OldDomain,$NewDomain)
    write-host ("Working on User: " + $user.userprincipalname) -fore Green
    Set-msoluserprincipalname -userprincipalname $user.userprincipalname -newuserprincipalname $NewUPN
    Get-msoluser -userprincipalname $user.NewUPN |select userprincipalname
}