Advertisement
Guest User

Untitled

a guest
Jan 15th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  # This command has 2 non-mandatory parametrs
  2. # Parameter 1: User-Account -> f.e "user1"
  3. # Parameter 2: New profiles path -> f.e "D:\USER_PROFILES" (default value is "D:\users")
  4. #
  5. # Execution examples
  6. #
  7. # PS> .\MOVE_PROFILES.ps1 (moves all user profiles to the new location D:\users\<user>"
  8. # PS> .\MOVE_PROFILES.ps1 user1 (moves user1 profile to the new location D:\users\<user>"
  9. # PS> .\MOVE_PROFILES.ps1 user1 G:\folder (moves user1 profile to the new location G:\FOLDER\<user>"
  10. # PS> .\MOVE_PROFILES.ps1 ALL G:\folder (moves all user profiles to the new location G:\FOLDER\<user>"
  11. #
  12. # Execute it as administrator when the user you want to migrate is not logged!!
  13.  
  14.  
  15. Param(
  16.   [string] $ACCOUNT = "ALL",
  17.   [string] $NEWPATH = "E:\Users"
  18. )
  19.  
  20. # Obtain all user profiles (excluding system profiles)
  21. $USER_PROFILES = dir -LiteralPath "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" | ? {$_.name -match "S-1-5-21-"}
  22.  
  23. # Loop to process all profiles
  24. foreach ($USER_PROFILE in $USER_PROFILES) {
  25.  
  26.     # Obtain registry, profile path, user and profile new path
  27.     $REGISTRY = $($($USER_PROFILE.pspath.tostring().split("::") | Select-Object -Last 1).Replace("HKEY_LOCAL_MACHINE","HKLM:"))
  28.     $OLD_PROFILEPATH = $(Get-ItemProperty -LiteralPath $REGISTRY -name ProfileImagePath).ProfileImagePath.tostring()
  29.     $USER=$OLD_PROFILEPATH.Split("\")[-1]
  30.     $NEW_PROFILEPATH = "$NEWPATH\$USER"
  31.    
  32.     # Process all or the user passed as parameter?
  33.     If ($ACCOUNT -eq "ALL" -or $USER -eq $ACCOUNT)
  34.     {
  35.         Write-Host "User:       $USER"
  36.         Write-Host "Registry:   $REGISTRY"
  37.         Write-Host "Old path:   $OLD_PROFILEPATH"
  38.         Write-Host "New path:   $NEW_PROFILEPATH"
  39.         Write-Host
  40.  
  41.         If ($OLD_PROFILEPATH -ne $NEW_PROFILEPATH)
  42.         {
  43.             # Change the profile path in the registry
  44.             Set-ItemProperty -LiteralPath $REGISTRY -Name ProfileImagePath -Value $NEW_PROFILEPATH
  45.             Write-Host "- Modified Windows registry (ProfileImagePath)"
  46.             Write-Host "- Moving folders to new location ($NEW_PROFILEPATH)..."
  47.  
  48.             # Move the profile folders to the new location
  49.             $ROBOCOPY_COMMAND = "robocopy /e /MOVE /copyall /r:0 /mt:4 /b /nfl /xj /xjd /xjf $OLD_PROFILEPATH $NEW_PROFILEPATH > robocopy_$USER.log"
  50.             Invoke-Expression $ROBOCOPY_COMMAND
  51.             Write-Host "- Done!"
  52.         }
  53.         else {
  54.             Write-Host "- Moving folders to new location ($NEW_PROFILEPATH)..."
  55.             $ROBOCOPY_COMMAND = "robocopy /e /MOVE /copyall /r:0 /mt:4 /b /nfl /xj /xjd /xjf $OLD_PROFILEPATH $NEW_PROFILEPATH > robocopy_$USER.log"
  56.             Write-Host "- Executing: $ROBOCOPY_COMMAND"
  57.             Invoke-Expression $ROBOCOPY_COMMAND
  58.             Write-Host "- Done!"
  59.         }
  60.         Write-Host "-------------------------------"       
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement