【O365 PowerShell Script】批量更改用戶名的域名

#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.dom

#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
}
相關文章
相關標籤/搜索