├── LICENSE ├── README.md ├── UserDataBackup.ps1 └── UserDataRestore.ps1 /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2020, Aaron Zercher 2 | 3 | Permission to use, copy, modify, and/or distribute this software for any 4 | purpose with or without fee is hereby granted, provided that the above 5 | copyright notice and this permission notice appear in all copies. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## PowerShell User Data Backup & Restore Scripts 2 | 3 | When I came into my current role in Desktop Support, we were using BATCH scripts to perform these tasks. However, I wanted to move them to PowerShell. 4 | 5 | #### My goals for creating this were: 6 | 7 | 1. Keep the process simple 8 | 2. Provide seamless transition for my team 9 | 3. Backup and Restore the same information as before 10 | 11 | #### What does the script backup and restore: 12 | 13 | 1. All user data (Desktop, Documents, Downloads, Favorites, Pictures) 14 | 2. Browser data from Google Chrome and Firefox 15 | 3. Network printer information and Mapped Network drives 16 | 17 | Code snippet: 18 | 19 | ```PowerShell 20 | #region DeclaringDataBackupSources 21 | $folder = "Desktop", 22 | "Downloads", 23 | "Favorites", 24 | "Documents", 25 | "Pictures", 26 | "AppData\Local\Google\Chrome\User Data", 27 | "AppData\Local\Mozilla\Firefox\Profiles", 28 | "AppData\Roaming\Mozilla\Firefox" 29 | #endregion DeclaringDataBackupSources 30 | ``` 31 | 32 | #### What's been added 33 | 34 | I added a little piece to the backup script to clear all browser cache to allow the backup time to decrease. 35 | 36 | Here is a snippet of that code: 37 | 38 | ```Power 39 | ##region CacheFolderDeletion Variables (Currently supports Chrome and Firefox) 40 | $delete = "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Cache\", 41 | "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Code Cache\", 42 | "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Service Worker\ScriptCache", 43 | "$env:LOCALAPPDATA\Google\Chrome\User Data\Profile 1\Cache\", 44 | "$env:LOCALAPPDATA\Google\Chrome\User Data\Profile 1\Code Cache\", 45 | "$env:LOCALAPPDATA\Google\Chrome\User Data\Profile 1\Service Worker\ScriptCache", 46 | "$env:LOCALAPPDATA\Mozilla\Firefox\Profiles\*.default\cache", 47 | "$env:LOCALAPPDATA\Mozilla\Firefox\Profiles\*.default\cache2" 48 | ##endregion CacheFolderDeletion Variables 49 | 50 | ##region DeleteFiles 51 | Write-Host -ForegroundColor Cyan "Begin Browser Cleanup" 52 | Get-ChildItem -Path $delete | Remove-Item -Verbose -Recurse 53 | ##endregion 54 | ``` 55 | 56 | The script has been tested on Windows 7 and Windows 10 and is 100% working. 57 | 58 | #### Variables to setup 59 | 60 | For the Backup script: 61 | 62 | ```PowerShell 63 | $destination 64 | ``` 65 | 66 | For the Restore script: 67 | 68 | ```PowerShell 69 | $source 70 | ``` 71 | 72 | 73 | 74 | **Please feel free to use and modify the script to suit your needs.** 75 | -------------------------------------------------------------------------------- /UserDataBackup.ps1: -------------------------------------------------------------------------------- 1 | ## 2 | ######## Powershell Script for Backing up User Data ######## 3 | ## 4 | ######## Also Deletes Browser Cache ######## 5 | ## 6 | ######## v5.0 7/20/20 ######## 7 | ## 8 | ######## By Aaron Zercher ######## 9 | ## 10 | ######## Script will backup all data in Desktop, Documents, Downloads, Favorites, Pictures, Chrome, and Mozilla Data ######## 11 | ## 12 | ##region CacheFolderDeletion Variables (Currently supports Chrome and Firefox) 13 | $delete = "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Cache\", 14 | "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Code Cache\", 15 | "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Service Worker\ScriptCache", 16 | "$env:LOCALAPPDATA\Google\Chrome\User Data\Profile 1\Cache\", 17 | "$env:LOCALAPPDATA\Google\Chrome\User Data\Profile 1\Code Cache\", 18 | "$env:LOCALAPPDATA\Google\Chrome\User Data\Profile 1\Service Worker\ScriptCache", 19 | "$env:LOCALAPPDATA\Mozilla\Firefox\Profiles\*.default\cache", 20 | "$env:LOCALAPPDATA\Mozilla\Firefox\Profiles\*.default\cache2" 21 | ##endregion CacheFolderDeletion Variables 22 | 23 | ##region DeleteFiles 24 | Write-Host -ForegroundColor Cyan "Begin Browser Cleanup" 25 | Get-ChildItem -Path $delete | Remove-Item -Verbose -Recurse 26 | ##endregion 27 | 28 | ##region Switch to Backup 29 | Start-Sleep -Seconds 30 30 | Write-Host "" 31 | Write-Host "" 32 | Write-Host "" 33 | Write-Host -ForegroundColor Yellow "Standby Loading Backup Script..." 34 | Start-Sleep -Seconds 10 35 | Clear-Host 36 | ##end region Switch to Backup 37 | 38 | #region GetVariables 39 | [String]$Technician = Read-Host -Prompt "What Technician is auditing this computer (First name only)" 40 | [String]$NetworkPassword = Read-Host -Prompt "Please enter the Users network password" ##NEEDED 41 | #endregion GetVariables 42 | 43 | #region DeclaringBackuplocation 44 | $destination = "(Your Destination here)" 45 | #endregion DeclaringBackuplocation 46 | 47 | #region DeclaringDataBackupSources 48 | $folder = "Desktop", 49 | "Downloads", 50 | "Favorites", 51 | "Documents", 52 | "Pictures", 53 | "AppData\Local\Google\Chrome\User Data", 54 | "AppData\Local\Mozilla\Firefox\Profiles", 55 | "AppData\Roaming\Mozilla\Firefox" 56 | #endregion DeclaringDataBackupSources 57 | 58 | ###### Backup Data section ######## 59 | Write-Host "" 60 | Write-Host "" 61 | Write-Host "" 62 | Write-Host -ForegroundColor green "Backing up data from local machine for $env:USERNAME" 63 | 64 | #region Folder creation 65 | New-Item -Type Directory -Path $destination -Name "Audit Information" -Force | Out-Null 66 | New-Item -Type Directory -Path $destination -Name "RegistryInformation" -Force | Out-Null 67 | New-Item -Type Directory -Path $destination -Name "RegistryInformation\Temp" -Force | Out-Null 68 | #endregion Folder creation 69 | 70 | #region DataBackup 71 | ForEach ($f in $folder) { 72 | $currentLocalFolder = Join-Path -Path $env:USERPROFILE -ChildPath $f 73 | $currentRemoteFolder = Join-Path -Path $destination -ChildPath $f 74 | $currentFolderSize = (Get-ChildItem -ErrorAction silentlyContinue $currentLocalFolder -Recurse -Force | Measure-Object -ErrorAction silentlyContinue -Property Length -Sum ).Sum / 1MB 75 | $currentFolderSizeRounded = [System.Math]::Round($currentFolderSize) 76 | Write-Host -ForegroundColor cyan " $f... ($currentFolderSizeRounded MB)" 77 | Copy-Item -ErrorAction silentlyContinue -recurse $currentLocalFolder $currentRemoteFolder 78 | } 79 | #endregion DataBackup 80 | 81 | #Yes this is unsecure but needs to be done. 82 | ##region PasswordFileCreation 83 | Out-file -filepath "$destination\Audit Information\NewBuild.txt" -inputobject $NetworkPassword 84 | ##endregion PasswordFileCreation 85 | 86 | ##region Registry Backup 87 | Write-Host -ForegroundColor green "Backing up registry data from local machine for $env:USERNAME" 88 | $keys = "HKCU\Network", "HKCU\Printers", "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices", "HKCU\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts", "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows" 89 | 90 | $tempFolder = "$destination\RegistryInformation\temp" 91 | $outputFile = "$destination\RegistryInformation\RegBackup.reg" 92 | 93 | $keys | ForEach-Object { 94 | $i++ 95 | & reg export $_ "$tempFolder\$i.reg" 96 | } 97 | 98 | "Windows Registry Editor Version 5.00" | Set-Content $outputFile 99 | Get-Content "$tempFolder\*.reg" | Where-Object { 100 | $_ -ne "Windows Registry Editor Version 5.00" 101 | } | Add-Content $outputFile 102 | ##endregion Registry Backup 103 | Write-Host "" 104 | Write-Host -ForegroundColor green "Registry Backup Complete" 105 | Write-Host "" 106 | Write-Host "" 107 | Write-Host -ForegroundColor green "Backup complete! You my shutdown the computer" 108 | Start-Sleep -Seconds 15 109 | 110 | ##region shutdownPC 111 | Stop-Computer -Confirm 112 | ##endregion shutdownPC 113 | 114 | ##Script completes 115 | -------------------------------------------------------------------------------- /UserDataRestore.ps1: -------------------------------------------------------------------------------- 1 | ## 2 | ######## Powershell Script for Restoring User Data ######## 3 | ## 4 | ######## v4.0 7/20/20 ######## 5 | ## 6 | ######## By Aaron Zercher ######## 7 | ## 8 | ######## Script will restore all data in Desktop, Documents, Downloads, Favorites, Pictures, Chrome, and Mozilla Data ######## 9 | ## 10 | ##Declares the values and prompts for Technician information 11 | $Technician = Read-Host -Prompt "What Technician is auditing this computer (First name only)" 12 | 13 | ##Declares the Restore location ######## 14 | $source = "(Your Source)" 15 | 16 | ##region Declaring Data to backup 17 | $folder = "Desktop", 18 | "Downloads", 19 | "Favorites", 20 | "Documents", 21 | "Pictures" 22 | ##Folder 2 is used for Browser data 23 | $folder2 = "AppData\Local\Google\Chrome\", 24 | "AppData\Local\Mozilla\Firefox\Profiles", 25 | "AppData\Roaming\Mozilla\Firefox" 26 | #endregion DeclaringDataRestoreSources 27 | 28 | ###### Restore Data section ######## 29 | write-host -ForegroundColor green "Restoring data to local machine for $env:USERNAME" 30 | 31 | foreach ($f in $folder) 32 | { 33 | $currentLocalFolder = $env:USERPROFILE + "\" 34 | $currentRemoteFolder = $source + "\" + $f 35 | $currentFolderSize = (Get-ChildItem -ErrorAction silentlyContinue $currentRemoteFolder -Recurse -Force | Measure-Object -ErrorAction Inquire -Property Length -Sum ).Sum / 1MB 36 | $currentFolderSizeRounded = [System.Math]::Round($currentFolderSize) 37 | write-host -ForegroundColor Magenta " $f... ($currentFolderSizeRounded MB)" 38 | Copy-Item -Force -recurse $currentRemoteFolder $currentLocalFolder 39 | } 40 | 41 | foreach ($f2 in $folder2) 42 | { 43 | $currentLocalFolder = Join-Path $env:USERPROFILE (Split-Path $f2) 44 | $currentRemoteFolder = $source + "\" + $f2 45 | $currentFolderSize = (Get-ChildItem -ErrorAction silentlyContinue $currentRemoteFolder -Recurse -Force | Measure-Object -ErrorAction Inquire -Property Length -Sum ).Sum / 1MB 46 | $currentFolderSizeRounded = [System.Math]::Round($currentFolderSize) 47 | write-host -ForegroundColor Magenta " $f2... ($currentFolderSizeRounded MB)" 48 | Robocopy.exe /mir $currentRemoteFolder $currentLocalFolder 49 | } 50 | 51 | ######## Begin Registry Restore ######## 52 | $RegistryRestore = Join-Path -Path $Source -ChildPath "RegistryInformation\RegBackup.reg" 53 | If (Test-Path -Path $RegistryRestore) { 54 | Try { 55 | & Reg import "$RegistryRestore" 56 | If ($LastExitCode -NE "0") { 57 | Break 58 | # See https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/reg-import#remarks 59 | } # END If LastExistCode NE 0 60 | } # END Try Reg Import 61 | Catch { 62 | Write-Warning -Message "Critical error/failure when attempting to import registry-backup!" 63 | } # END Catch Reg Import 64 | } # END If Test-Path RegistryBackup 65 | Else { 66 | Write-Warning -Message "Registry Backup not found at "$($RegistryRestore)"!" 67 | } # END Else Test-Path RegistryBackup 68 | 69 | write-host -ForegroundColor green "Restore complete! Select Yes to Reboot the Computer" 70 | 71 | Restart-Computer -confirm 72 | --------------------------------------------------------------------------------