title: Active directory migration with powershell
body: <p>I need to migrate from AD Windows2003Forest to AD 2016. I have below script to create users in bulk. My requirement is to map the same SID of older AD to new AD. For example in older AD SID='xyz' then it should be the same in newAD too as SID='xyz'</p>
  
  <p>I am having all the users data along with SID in CSV format &amp; am using below PowerShell script which is somehow not working. As of advice or suggestions.</p>
  
  <p>powershell code snippent:</p>
  
  <pre><code>#Enter a path to your import CSV file
  $ADUsers = Import-csv C:\scripts\newusers.csv
  
  foreach ($User in $ADUsers)
  {
  
         $Username    = $User.username
         $Password    = $User.password
         $Firstname   = $User.firstname
         $Lastname    = $User.lastname
         $Department = $User.department
         $OU           = $User.ou
         $sid     = $User.sid
      $UserPrincipalName = $User.UserPrincipalName
      $DistinguishedName = $User.DistinguishedName
  
         #Check if the user account already exists in AD
         if (Get-ADUser -F {SamAccountName -eq $Username})
         {
                 #If user does exist, output a warning message
                 Write-Warning "A user account $Username has already exist in Active Directory."
         }
         else
         {
                #If a user does not exist then create a new user account
  
          #Account will be created in the OU listed in the $OU variable in the CSV file; don’t forget to change the domain name in the"-UserPrincipalName" variable
                New-ADUser `
              -SamAccountName $Username `
              -UserPrincipalName $UserPrincipalName `
              -Name "$Firstname $Lastname" `
              -GivenName $Firstname `
              -Surname $Lastname `
              -Enabled $True `
              -ChangePasswordAtLogon $True `
              -DisplayName "$Lastname, $Firstname" `
              -Department $Department `
          -DistinguishedName $DistinguishedName `
          -SID $sid `
              -Path $OU `
              -AccountPassword (convertto-securestring $Password -AsPlainText -Force)
  
         }
  }
  </code></pre>