Skip to content

Instantly share code, notes, and snippets.

@gengor-git
Last active June 13, 2024 09:00
Show Gist options
  • Save gengor-git/45301e2e46364afb178b2fea69912fa5 to your computer and use it in GitHub Desktop.
Save gengor-git/45301e2e46364afb178b2fea69912fa5 to your computer and use it in GitHub Desktop.
Backup script to store my Windows configs in a 7-zip archive incl. scripts for chocolatey and VS Code.
<#PSScriptInfo
.AUTHOR
Martin Palmowski
.SYNPOSIS
Backup settings and configs for my Windows 10 environment.
.DESCRIPTION
Backup the most relevant settings and configs in a 7-zip archive.
Requires 7-zip to be installed an in the path.
Paths are based on Windows 10 defaults.
#>
param (
[CmdletBinding()]
[ValidateScript({
if (-Not ($_ | Test-Path)) {
throw "Folder does not exist."
}
if (-Not ($_ | Test-Path -PathType Container)) {
throw "The Path argument must be a folder. Files are not allowed."
}
return $true
})]
[System.IO.FileInfo]$DestinationFolder = $env:ONEDRIVECOMMERCIAL + "\Desktop",
[ValidateScript({
if (-Not ($_ | Test-Path)) {
throw "7-zip executable not found"
}
})]
[System.IO.FileInfo]$7zip = "C:\Program Files\7-Zip\7z.exe"
)
# These files are beeing generated temporarily.
$PuttyExport = "$env:ONEDRIVECOMMERCIAL\Desktop\Putty-Settings.reg"
$WinSCPExport = "$env:ONEDRIVECOMMERCIAL\Desktop\WinSCP-Settings.reg"
$PSScripts = "`"" + (Split-Path $PROFILE) + "\Scripts`""
# The files and folders we want in the backup. Modify here if needed.
$SettingsToBackup = @(
"$env:APPDATA\Code\User\settings.json", # VS Code
"$env:APPDATA\Code\User\keybindings.json",
"$env:APPDATA\Code\User\snippets",
"$env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json", # Windows Terminal settings
"`"$PROFILE`"", # PowerShell profile settings
$PSScripts, # PowerShell scripts
"`"$env:ONEDRIVECOMMERCIAL\Documents\Meine Datenquellen`"", # Meine Datenquellen for Excel data connections, may be different for your locale.
"$env:USERPROFILE\.gitconfig", # Your local git settings,
"$env:USERPROFILE\.ssh\config", # Config settings, e.g. for adapting the algorithms
"$env:APPDATA\KeePassXC", # KeePassXC settings
"$env:APPDATA\XMind\workspace-cathy", # XMind custom settings
"$env:APPDATA\.emacs.d\init.el", # Emacs settings directory
"$env:APPDATA\Notepad++", # Editor-settings for Notepad++
"$env:APPDATA\yWorks\yEd\palettes", # yEd palettes (e.g my custom colors, etc.)
"$env:APPDATA\Greenshot\Greenshot.ini", # Settings for capture and folders
"$env:APPDATA\FileZilla", # Filezilla settings incl. server list
"$env:LOCALAPPDATA\FreeCommanderXE", # Filemanager settings
"$env:LOCALAPPDATA\Microsoft\PowerToys", # Settings for Microsoft's PowerToys tools
"$env:APPDATA\SAP\Common", # SAP History [Haben ist besser als brauchen.]
"`"$env:APPDATA\SAP\SAP GUI`"", # SAP History [Haben ist besser als brauchen.]
"`"$PuttyExport`"", # The putty config export
"`"$WinSCPExport`"" # The WinSCP config export
)
# We use the date and time in the filename of the archive.
# This way you can run it regularily (e.g. via Scheduler)
# and not overwrite the old backup.
$FilenameDate = Get-Date -Format "yyyy_MM_dd-HH_mm"
$ArchiveFileName = "$DestinationFolder\Settings-backup-$FilenameDate.7z"
Write-Host " _ _ _ __ _ _ _ __ _"
Write-Host "| |__ __ _ ___ | | __ _ _ | '_ \     ___ ___ | |_ | |_ (_) _ __ / _`` | ___"
Write-Host "| '_ \ / _`` | / __|| |/ /| | | || |_) |   / __| / _ \| __|| __|| || '_ \ | (_| |/ __|"
Write-Host "| |_) || (_| || (__ | < | |_| || .__/    \__ \| __/| |_ | |_ | || | | | \__, |\__ \"
Write-Host "|_.__/ \__,_| \___||_|\_\ \__,_||_|    |___/ \___| \__| \__||_||_| |_| |___/ |___/"
Write-Host ""
Write-Host "Creating backup for these files and folders:"
$SettingsToBackup | ForEach-Object { Write-Host "-> $_" }
Write-Host "Backup will be saved to to $DestinationFolder."
# Export Putty settings as Registry text file.
Write-Host "Exporting Putty regristry settings."
Start-Process -FilePath reg.exe -Argument "export HKEY_CURRENT_USER\Software\SimonTatham `"$PuttyExport`"" -NoNewWindow -Wait
# Export WinSCP settings as registry text file.
# Note that you could change WinSCP to save it to a file on the hdd. But default would be Registry.
Write-Host "Exporting WinSCP registry settings."
Start-Process -FilePath reg.exe -Argument "export `"HKEY_CURRENT_USER\Software\Martin Prikryl\WinSCP 2`" `"$WinSCPExport`"" -NoNewWindow -Wait
# spf2 = absolute paths without drive letters; requires an up-to-date 7-zip version
Write-Host "Creating archive $ArchiveFileName."
$7zipArguments = "a `"$ArchiveFileName`" -spf2 $SettingsToBackup -xr!Cache -xr!Logs -xr!LogsModuleInterface"
# Create the 7-zip archive
Start-Process -FilePath $7zip -ArgumentList $7zipArguments -NoNewWindow -Wait
# Clean-up temp files
Remove-Item -Path "$PuttyExport" -Force -ErrorAction SilentlyContinue
Remove-Item -Path "$WinSCPExport" -Force -ErrorAction SilentlyContinue
# This launches the explorer and selects the created archive 7zip file.
Start-Process -FilePath explorer.exe -Argument "/select,$ArchiveFileName"
# & explorer.exe /select,$ArchiveFileName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment