Functions/Backup-MySettings.ps1


function Backup-MySettings {
    [CmdletBinding()]
    param (
        [Parameter()] [ValidateSet("VsCode", "WindowsTerminal", "Notepad++")] [string[]] $App,
        [Parameter()] [switch] $All
    )

    $AppList = [PSCustomObject]@{

        VsCode          = @("$($env:APPDATA)\Code\User\settings.json"
            "$($env:APPDATA)\Code\User\snippets\powershell.json")

        WindowsTerminal = "$($env:LOCALAPPDATA)\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState"

        "Notepad++"     = "$($env:APPDATA)\Notepad++\config.xml"

    }


    if ($All) {
        $App = $AppList.psobject.Properties.Name
    }

    foreach ($a in $App) {


        $SettingsPath = $AppList.$a

        $BackupPath = "$($env:OneDrive)\Backup\$($a)\$($env:computername)"
        # $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"
                "$path > $BackupPath2" | Out-File $InfoFile -Append

            } else {

                Write-Warning "Settings file not found at `"$($path)"

            }
        }

        # "-------------------------"

        # $SettingsPath
        # $SettingsFile
        # $BackupPath

    }
}