Public/Restore-EdgeProfile.ps1
Function Restore-EdgeProfile { <# .SYNOPSIS Restores edge profile from backup zip archive. THIS CODE IS MADE AVAILABLE AS IS, WITHOUT WARRANTY OF ANY KIND. THE ENTIRE RISK OF THE USE OR THE RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER. .DESCRIPTION Restores Microsoft Edge profile and reg keys from the zip archive. .NOTES Initial draft. .INPUTS Zip archive of backup profile and reg keys. .EXAMPLE .\Restore-EdgeProfile.ps1 -BackupZip 'C:\myfilepath\backup.zip' #> #==================================================================================================== # Requires #==================================================================================================== #region Requires #Requires -Version 5.0 #-- Requires -ShellId <ShellId> #-- Requires -RunAsAdministrator #-- Requires -PSSnapin <PSSnapin-Name> [-Version <N>[.<n>]] #endregion Requires #==================================================================================================== # Parameters #==================================================================================================== #region Parameters [cmdletbinding()] param( [Parameter(Mandatory = $true)] [ValidateSet('Stable', 'Beta', 'Dev', 'Canary')] [string]$Channel, [Parameter(Mandatory = $true)] [string]$BackupZip ) #endregion Parameters #==================================================================================================== # Initialize #==================================================================================================== #region Initialize $ErrorActionPreference = 'Stop' $isMSEdgeRunning = Get-Process -Name 'msedge' -ErrorAction Continue 2>$null $tempPath = "$env:TEMP\edgebackup" $destinationFolder = $BackupZip.Split('\')[-1] $destinationFolder = $destinationFolder.Split('.')[0] #endregion Initialize #==================================================================================================== # Main #==================================================================================================== #region Main if ($null -ne $isMSEdgeRunning) { Write-Host 'Microsoft Edge is running. Please exit and run again.' -ForegroundColor 'Red' exit } else { Write-Verbose 'Checking temp directory.' if (!(Test-Path -Path $tempPath)) { Write-Verbose 'No temp directory found, creating directory.' New-Item -Path $tempPath -ItemType 'Directory' | Out-Null } else { Write-Verbose 'Temp directory found, removing any files from previous runs.' Remove-Item -Path "$tempPath\*" -Recurse -Force } } # Expand archive Write-Verbose 'Expanding zip archive to temp folder.' try { Expand-Archive -Path $BackupZip -DestinationPath "$tempPath\$destinationFolder\" } catch { Write-Error -Message 'Error expanding backup archive to temp folder.' Exit } Write-Verbose 'Backing up current edge profile to zip file.' switch ($channel) { Stable { Compress-Archive -Path "$($env:LOCALAPPDATA)\Microsoft\Edge\*" -DestinationPath "$HOME\Downloads\edge_current_backup.zip" -CompressionLevel Fastest } Beta { Compress-Archive -Path "$($env:LOCALAPPDATA)\Microsoft\Edge Beta\*" -DestinationPath "$HOME\Downloads\edge_current_backup.zip" -CompressionLevel Fastest } Dev { Compress-Archive -Path "$($env:LOCALAPPDATA)\Microsoft\Edge Dev\*" -DestinationPath "$HOME\Downloads\edge_current_backup.zip" -CompressionLevel Fastest } Canary { Compress-Archive -Path "$($env:LOCALAPPDATA)\Microsoft\Edge SxS\*" -DestinationPath "$HOME\Downloads\edge_current_backup.zip" -CompressionLevel Fastest } } Write-Verbose 'Backing up current edge windows registry keys.' switch ($channel) { Stable { Invoke-Command { reg export 'HKEY_CURRENT_USER\SOFTWARE\Microsoft\Edge\PreferenceMACs' "$tempPath\edge_current.reg" } } Beta { Invoke-Command { reg export 'HKEY_CURRENT_USER\SOFTWARE\Microsoft\Edge Beta\PreferenceMACs' "$tempPath\edgebeta_current.reg" } } Dev { Invoke-Command { reg export 'HKEY_CURRENT_USER\SOFTWARE\Microsoft\Edge Dev\PreferenceMACs' "$tempPath\edgedev_current.reg" } } Canary { Invoke-Command { reg export 'HKEY_CURRENT_USER\SOFTWARE\Microsoft\Edge SxS\PreferenceMACs' "$tempPath\edgecanary_current.reg" } } } Write-Verbose 'Adding reg key to zip file.' Compress-Archive -Path "$tempPath\*.reg" -DestinationPath "$HOME\Downloads\edge_current_backup.zip" -CompressionLevel Fastest -Update Write-Verbose 'Removing current Edge profile' switch ($channel) { Stable { Remove-Item -Path "$($env:LOCALAPPDATA)\Microsoft\Edge\*" -Recurse -Force } Beta { Remove-Item -Path "$($env:LOCALAPPDATA)\Microsoft\Edge Beta\*" -Recurse -Force } Dev { Remove-Item -Path "$($env:LOCALAPPDATA)\Microsoft\Edge Dev\*" -Recurse -Force } Canary { Remove-Item -Path "$($env:LOCALAPPDATA)\Microsoft\Edge SxS\*" -Recurse -Force } } Write-Verbose 'Removing current Edge windows registry keys.' switch ($channel) { Stable { Invoke-Command { reg delete 'HKEY_CURRENT_USER\SOFTWARE\Microsoft\Edge\PreferenceMACs' /f } } Beta { Invoke-Command { reg delete 'HKEY_CURRENT_USER\SOFTWARE\Microsoft\Edge Beta\PreferenceMACs' /f } } Dev { Invoke-Command { reg delete 'HKEY_CURRENT_USER\SOFTWARE\Microsoft\Edge Dev\PreferenceMACs' /f } } Canary { Invoke-Command { reg delete 'HKEY_CURRENT_USER\SOFTWARE\Microsoft\Edge SxS\PreferenceMACs' /f } } } Write-Verbose 'Importing backed up reg keys.' switch ($channel) { Stable { Invoke-Command { reg import "$($tempPath)\$($destinationFolder)\edge.reg" } } Beta { Invoke-Command { reg import "$($tempPath)\$($destinationFolder)\edgebeta.reg" } } Dev { Invoke-Command { reg import "$($tempPath)\$($destinationFolder)\edgedev.reg" } } Canary { Invoke-Command { reg import "$($tempPath)\$($destinationFolder)\edgecanary.reg" } } } Write-Verbose 'Moving backed up profile into application folder.' switch ($channel) { Stable { Move-Item -Path "$tempPath\$destinationFolder\*" -Destination "$($env:LOCALAPPDATA)\Microsoft\Edge\" -Force } Beta { Move-Item -Path "$tempPath\$destinationFolder\*" -Destination "$($env:LOCALAPPDATA)\Microsoft\Edge Beta\*" -Force } Dev { Move-Item -Path "$tempPath\$destinationFolder\*" -Destination "$($env:LOCALAPPDATA)\Microsoft\Edge Dev\*" -Force } Canary { Move-Item -Path "$tempPath\$destinationFolder\*" -Destination "$($env:LOCALAPPDATA)\Microsoft\Edge SxS\*" -Force } } Write-Verbose 'Cleaning up exported files.' Remove-Item -Path $tempPath -Recurse -Force #endregion Main } |