Functions/Backup-MySettings.ps1
function Backup-MySettings { [CmdletBinding()] param ( [Parameter()] [ValidateSet("VsCode", "WindowsTerminal", "Notepad++")] [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" } if ($All) { $App = $AppList.psobject.Properties.Name } $RestoreCommands = "" $BackupRoot = "$($env:OneDrive)\Backup" $BackupPathNow = "$($BackupRoot)\$($env:COMPUTERNAME)_$(Get-Date -Format "yyyyMMdd-HHmmss")" #Remove old backups # $ErrorActionPreference = "SilentlyContinue" $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 # Remove-Item $f.FullName -Recurse -Force # gci $FullName -Recurse | Remove-Item -Force -Recurse # $FullName # $Items = Get-ChildItem -LiteralPath $FullName -Recurse # foreach ($Item in $Items) { # $Item.Delete() # } # sleep 1 # $Items = Get-Item -LiteralPath $FullName # $Items.Delete() } } # $ErrorActionPreference = "Continue" #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 } # $PathType | Format-List # $BackupPath2 Copy-Item $path $BackupPath2 -Recurse -Force 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" } |