0% found this document useful (0 votes)
44 views

User Profile Backup & Restore (For Machine Swaps)

This PowerShell script backs up or restores a user's profile folders and Outlook data to a network share when migrating a user to a new machine. It prompts the user to choose whether to backup or restore data. If restoring, it copies folders like Documents, Music, and AppData from the network share to the user's profile on the local machine. If backing up, it copies those same folders from the local machine to the network share for archiving.

Uploaded by

Tomtom Ukata
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

User Profile Backup & Restore (For Machine Swaps)

This PowerShell script backs up or restores a user's profile folders and Outlook data to a network share when migrating a user to a new machine. It prompts the user to choose whether to backup or restore data. If restoring, it copies folders like Documents, Music, and AppData from the network share to the user's profile on the local machine. If backing up, it copies those same folders from the local machine to the network share for archiving.

Uploaded by

Tomtom Ukata
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

##### User profile Backup & Restore (For machine swaps)

##### https://community.spiceworks.com/scripts/show_download/2640-user-profile-
backup-restore-for-machine-swaps

$destination = "\\SERVER\Share\backups"

$folder = "Desktop",
"Downloads",
"Favorites",
"Documents",
"Music",
"Pictures",
"Videos",
"AppData\Local\Mozilla",
"AppData\Local\Google",
"AppData\Roaming\Mozilla"

###################################################################################
############################

$username = gc env:username
$userprofile = gc env:userprofile
$appData = gc env:localAPPDATA

###### Restore data section ######


if ([IO.Directory]::Exists($destination + "\" + $username + "\"))
{

$caption = "Choose Action";


$message = "A backup folder for $username already exists, would you like to
restore the data to the local machine?";
$Yes = new-Object System.Management.Automation.Host.ChoiceDescription
"&Yes","Yes";
$No = new-Object System.Management.Automation.Host.ChoiceDescription
"&No","No";
$choices = [System.Management.Automation.Host.ChoiceDescription[]]($Yes,$No);
$answer = $host.ui.PromptForChoice($caption,$message,$choices,0)

if ($answer -eq 0)
{

write-host -ForegroundColor green "Restoring data to local machine for


$username"
foreach ($f in $folder)
{
$currentLocalFolder = $userprofile + "\" + $f
$currentRemoteFolder = $destination + "\" + $username + "\" + $f
write-host -ForegroundColor cyan " $f..."
Copy-Item -ErrorAction silentlyContinue -recurse
$currentRemoteFolder $userprofile

if ($f -eq "AppData\Local\Mozilla") { rename-item


$currentLocalFolder "$currentLocalFolder.old" }
if ($f -eq "AppData\Roaming\Mozilla") { rename-item
$currentLocalFolder "$currentLocalFolder.old" }
if ($f -eq "AppData\Local\Google") { rename-item
$currentLocalFolder "$currentLocalFolder.old" }
}
rename-item "$destination\$username" "$destination\$username.restored"
write-host -ForegroundColor green "Restore Complete!"
}

else
{
write-host -ForegroundColor yellow "Aborting process"
exit
}

###### Backup Data section ########


else
{

Write-Host -ForegroundColor green "Outlook is about to close, save any


unsaved emails then press any key to continue ..."

$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

Get-Process | Where { $_.Name -Eq "OUTLOOK" } | Kill

write-host -ForegroundColor green "Backing up data from local machine for


$username"

foreach ($f in $folder)


{
$currentLocalFolder = $userprofile + "\" + $f
$currentRemoteFolder = $destination + "\" + $username + "\" + $f
$currentFolderSize = (Get-ChildItem -ErrorAction silentlyContinue
$currentLocalFolder -Recurse -Force | Measure-Object -ErrorAction silentlyContinue
-Property Length -Sum ).Sum / 1MB
$currentFolderSizeRounded = [System.Math]::Round($currentFolderSize)
write-host -ForegroundColor cyan " $f... ($currentFolderSizeRounded
MB)"
Copy-Item -ErrorAction silentlyContinue -recurse $currentLocalFolder
$currentRemoteFolder
}

$oldStylePST = [IO.Directory]::GetFiles($appData + "\Microsoft\Outlook",


"*.pst")
foreach($pst in $oldStylePST)
{
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}
write-host -ForegroundColor yellow " $pst..."
Copy-Item $pst ($destination + "\" + $username + "\Documents\Outlook
Files\oldstyle")
}

write-host -ForegroundColor green "Backup complete!"

You might also like