Functions/Backup-MySettings.ps1
function Backup-MySettings { [CmdletBinding()] param ( [Parameter()] [ValidateSet("VsCode", "WindowsTerminal", "Notepad++", "PowerToys")] [string[]] $App, [Parameter()] [switch] $All, [Parameter()] [int] $RetentionDays = 14 ) $AppList = [PSCustomObject]@{ VsCode = @("$($env:APPDATA)\Code\User\settings.json" "$($env:APPDATA)\Code\User\snippets\powershell.json") WindowsTerminal = "$($env:LOCALAPPDATA)\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json" "Notepad++" = "$($env:APPDATA)\Notepad++\config.xml" # PowerToys = Get-ChildItem "$($env:LOCALAPPDATA)\Microsoft\PowerToys" -Recurse -Include *.json PowerToys = "$($env:LOCALAPPDATA)\Microsoft\PowerToys\*.json" } if ($All) { $App = $AppList.psobject.Properties.Name } $RestoreCommands = "" $BackupRoot = "$($env:OneDrive)\Backup" $BackupPathNow = "$($BackupRoot)\$($env:COMPUTERNAME)_$(Get-Date -Format "yyyyMMdd-HHmmss")" #Remove old backups $Folders = Get-ChildItem $BackupRoot foreach ($f in $folders) { if ($f.LastWriteTime -lt (Get-Date).AddDays(-$RetentionDays)) { $FullName = $f.FullName Move-Item $FullName "$($env:TEMP)\trash" Remove-Item "$($env:TEMP)\trash" -Recurse -Force } } #End remove old backups foreach ($a in $App) { $SettingsPath = $AppList.$a $BackupPath = "$($BackupPathNow)\$($a)" # $BackupPath = "C:\Temp\Backup\$($a)\$($env:computername)" # $InfoFile = "$($BackupPath)\info.txt" if (-not(Test-Path $BackupPath)) { New-Item $BackupPath -ItemType Directory | Out-Null } # $null | Out-File $InfoFile foreach ($path in $SettingsPath) { if (Test-Path $path) { $SettingsFile = (Get-Item $path).Name $PathType = ((Get-Item $path).GetType()).Name if ($PathType -eq "FileInfo") { $BackupPath2 = Join-Path $BackupPath $SettingsFile } else { $BackupPath2 = $BackupPath } # Copy-Item $path $BackupPath2 -Recurse -Force robocopy $path $BackupPath2 /NFL /NDL /MIR /W:1 /R:1 Write-Output "$path > $BackupPath2" $RestoreCommands += "Copy-Item `"$BackupPath2`" `"$path`" -Recurse -Force`n" } else { Write-Warning "Settings file not found at `"$($path)" } } # "-------------------------" # $SettingsPath # $SettingsFile # $BackupPath } $RestoreCommands | Out-File "$($BackupPathNow)\Restore_$($env:computername).ps1" } |