Previously I posted about While that script did work for my purposes it was not streamlined and didn’t do a lot of error checking and just generally wasn’t that clean. It was more like a battering ram. The actual act of using the move-spuser cmdlet is really pretty simple.

  1. Define the old userID.
  2. Define the new userID.
  3. Get-Spweb for a site where you know the user object exists.
  4. Create a SPUser object to feed into the move-SPuser cmdlet.
  5. If a SPuser object was found confirm the new userID and call move-spuser.
#initialize variables
$web = $null
$user = $null
$newuserID = $null
$OlduserID = $null

#Change IDs as needed
$OlduserID = "i:0#.w|olddomain\oldusername"
$newuserID = "i:0#.w|newdomain\newusername"

$web = Get-SPWeb ""

$user = Get-SPUser -Web $web -Identity $OlduserID #-ErrorAction SilentlyContinue

If($user) {
    $newuserID = $web.EnsureUser("$newuserID")
    Move-SPUser -Identity $user -NewAlias $NewuserID -Confirm:$false -IgnoreSID

Once Move-SPUser is called it will change all instances of oldusername to newusername. Also if you plan to do a few of these kinds of moves in a row (or do a mass migration) its always a good idea to null out your variables each time (trust me on this).

While this is much cleaner in terms of how to change a users identity, it only does it for one user. My next project is to rewrite my previous script with a cleaner process for doing a mass domain migration.