Open PowerShell window in your system.
Paste the below code in your PowerShell window.
Run the script.
Add-PSSnapin Microsoft.SharePoint.PowerShell
- #Import data from CSV file
- $UserData = Import-CSV -path "C:\GowthamTestuser.csv"
-
- #Iterate through each Row in the CSV
- foreach ($Row in $UserData)
- {
- write-host "Processing user:" $row.Email
-
- #Site collection URL
- $siteURL ="https://gowthamSPDEV.microsoft.com"
- $site = Get-SPSite $siteURL
-
- foreach($web in $site.AllWebs)
- {
- #Get All Users
- $UserColl = Get-SPUser -web $web.Url
-
- foreach ($User in $UserColl)
- {
- #Get values from CSV File
- $OldUserID= $Row.OldUserID.Trim()
- $NewUserID =$Row.NewUserID.Trim()
- $Email = $Row.Email.Trim()
-
- #Search for Old User Accounts
- if($User.UserLogin.Contains($OldUserID))
- {
- #Update the User E-mail
- Set-SPUser -Identity $User.UserLogin -Email $Email -Web $web.URL
-
- $NewUser = $User.UserLogin.replace($OldUserID, $NewUserID)
-
- #Migrate user from Old account to new account - migrate users to new domain
- Move-SPUser -Identity $User -NewAlias $NewUser -IgnoreSID -confirm:$false
- write-host "User Migrated: $($User.userlogin) at site $($web.Url)"
- }
-
- }
- }
- }
This PowerShell script migrates users to new domain programmatically.