Advertisement
Guest User

backup/restore user profile

a guest
Nov 17th, 2017
2,799
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $destination = "YOUR FILE PATH HERE"
  2.  
  3. $folder = "Desktop",
  4. "Downloads",
  5. "Favorites",
  6. "Documents",
  7. "Music",
  8. "Pictures",
  9. "Videos",
  10. "AppData\Local\Mozilla",
  11. "AppData\Local\Google",
  12. "AppData\Roaming\Mozilla"
  13.  
  14. ###############################################################################################################
  15.  
  16. $username = gc env:username
  17. $userprofile = gc env:userprofile
  18. $appData = gc env:localAPPDATA
  19.  
  20.  
  21. ###### Restore data section ######
  22. if ([IO.Directory]::Exists($destination + "\" + $username + "\"))
  23. {
  24.  
  25.     $caption = "Choose Action";
  26.     $message = "A backup folder for $username already exists, would you like to restore the data to the local machine?";
  27.     $Yes = new-Object System.Management.Automation.Host.ChoiceDescription "&Yes","Yes";
  28.     $No = new-Object System.Management.Automation.Host.ChoiceDescription "&No","No";
  29.     $choices = [System.Management.Automation.Host.ChoiceDescription[]]($Yes,$No);
  30.     $answer = $host.ui.PromptForChoice($caption,$message,$choices,0)
  31.  
  32.     if ($answer -eq 0)
  33.     {
  34.        
  35.         write-host -ForegroundColor green "Restoring data to local machine for $username"
  36.         foreach ($f in $folder)
  37.         {  
  38.             $currentLocalFolder = $userprofile + "\" + $f
  39.             $currentRemoteFolder = $destination + "\" + $username + "\" + $f
  40.             write-host -ForegroundColor cyan "  $f..."
  41.             Copy-Item -ErrorAction silentlyContinue -recurse $currentRemoteFolder $userprofile
  42.            
  43.             if ($f -eq "AppData\Local\Mozilla") { rename-item $currentLocalFolder "$currentLocalFolder.old" }
  44.             if ($f -eq "AppData\Roaming\Mozilla") { rename-item $currentLocalFolder "$currentLocalFolder.old" }
  45.             if ($f -eq "AppData\Local\Google") { rename-item $currentLocalFolder "$currentLocalFolder.old" }
  46.        
  47.         }
  48.         rename-item "$destination\$username" "$destination\$username.restored"
  49.         write-host -ForegroundColor green "Restore Complete!"
  50.     }
  51.    
  52.     else
  53.     {
  54.         write-host -ForegroundColor yellow "Aborting process"
  55.         exit
  56.     }
  57.    
  58.    
  59. }
  60.  
  61. ###### Backup Data section ########
  62. else
  63. {
  64.        
  65.     Write-Host -ForegroundColor green "Outlook is about to close, save any unsaved emails then press any key to continue ..."
  66.  
  67.     $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
  68.    
  69.     Get-Process | Where { $_.Name -Eq "OUTLOOK" } | Kill
  70.  
  71.     write-host -ForegroundColor green "Backing up data from local machine for $username"
  72.    
  73.     foreach ($f in $folder)
  74.     {  
  75.         $currentLocalFolder = $userprofile + "\" + $f
  76.         $currentRemoteFolder = $destination + "\" + $username + "\" + $f
  77.         $currentFolderSize = (Get-ChildItem -ErrorAction silentlyContinue $currentLocalFolder -Recurse -Force | Measure-Object -ErrorAction silentlyContinue -Property Length -Sum ).Sum / 1MB
  78.         $currentFolderSizeRounded = [System.Math]::Round($currentFolderSize)
  79.         write-host -ForegroundColor cyan "  $f... ($currentFolderSizeRounded MB)"
  80.         Copy-Item -ErrorAction silentlyContinue -recurse $currentLocalFolder $currentRemoteFolder
  81.     }
  82.    
  83.    
  84.    
  85.     $oldStylePST = [IO.Directory]::GetFiles($appData + "\Microsoft\Outlook", "*.pst")
  86.     foreach($pst in $oldStylePST)  
  87.     {
  88.         if ((test-path -path ($destination + "\" + $username + "\Documents\Outlook Files\oldstyle")) -eq 0){new-item -type directory -path ($destination + "\" + $username + "\Documents\Outlook Files\oldstyle") | out-null}
  89.         write-host -ForegroundColor yellow "  $pst..."
  90.         Copy-Item $pst ($destination + "\" + $username + "\Documents\Outlook Files\oldstyle")
  91.     }    
  92.    
  93.     write-host -ForegroundColor green "Backup complete!"
  94.    
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement