Migrate Users / Groups – PowerShell Script

You might have seen the similar script in other blogs for migrating users / groups. Here is my contribution for the same requirement. You can use this script for migrating bulk AD users and AD groups in a single shot. I have created this script to read the information from CSV file. You can find the actual script and two sample CSV files below of this post. My customer’s scenario was, they have upgraded their SharePoint 2007 to SharePoint Server 2010 but their domain changed in SharePoint Server 2010 so they have to update AD user and group information in the SharePoint databases by running the MigrateUserAccount & MigrateGroup method.

SPFarm.MigrateUserAccount is same as the STSADM migrateuser command that we all were familiar. This method migrate user account in SharePoint Foundation to a new login name and binary ID. If an entry for the new login name already exists, it is deleted to allow this change. The following entities are updated in the entire server farm: site collection users in the UserInfo tables, people lists, and security policies.

 Add-PSSnapin Microsoft.SharePoint.PowerShell
 function MigrateUserOrGroups($migrationType, $csvFile)
 {
    #Getting the SPFarm object
    $farm = Get-SPFarm
  
    Write-Host $migrationType
    #Checking whether the user input the type of Migration as Group
    if($migrationType -eq "Group"){
    Import-Csv $csvFile | ForEach-Object{
       Write-Host "Migrating Group" $_.oldlogin "to" $_.newlogin -ForegroundColor Green
       $farm.MigrateGroup($_.oldlogin, $_.newlogin)
       
        }
       }
       
     #Checking whether the user input the type of Migration as User
     if($migrationType -eq "User")
       {
         
         Import-Csv $csvFile | ForEach-Object{
         Write-Host "Migrating User" $_.oldlogin "to" $_.newlogin -ForegroundColor Green
         $farm.MigrateUserAccount( $_.oldlogin, $_.newlogin, $false )
         }      
       }
       
    Write-Host "Migration Completed" -ForegroundColor Cyan
    
    
    # $farm.Name
 }
  
 MigrateUserOrGroups $args[0] $args[1]

 

You can download the above PowerShell Script and sample test CSV files from the below link

 

Script and Test Files