Save and Restore VS Code Settings with PowerShell

Save VS Code Settings

Back up your VS Code configuration (settings, keybindings, and snippets) to any folder:

param(
    [string]$destinationPath = "."
)

## Resolve full path
$destinationPath = Resolve-Path -Path $destinationPath

## Define the source directory
$sourcePath = "$env:APPDATA\Code\User"

## Ensure the destination folder exists
if (!(Test-Path -Path $destinationPath)) {
    New-Item -ItemType Directory -Path $destinationPath | Out-Null
}

## Copy settings.json, keybindings.json, and snippets if they exist
$files = @("settings.json", "keybindings.json")
$snippetsPath = "$sourcePath\snippets"

foreach ($file in $files) {
    $filePath = "$sourcePath\$file"
    if (Test-Path -Path $filePath) {
        Copy-Item -Path $filePath -Destination $destinationPath -Force
        Write-Host "Copied $file to $destinationPath"
    } else {
        Write-Host "$file not found, skipping..."
    }
}

## Copy the snippets folder if it exists
if (Test-Path -Path $snippetsPath) {
    Copy-Item -Path $snippetsPath -Destination $destinationPath -Recurse -Force
    Write-Host "Copied snippets folder to $destinationPath"
} else {
    Write-Host "Snippets folder not found, skipping..."
}

Write-Host "VS Code settings backup completed in $destinationPath"

Restore VS Code Settings

Restore your VS Code configuration from a backup folder. Your current settings are automatically backed up before restoration:

param(
    [string]$sourcePath = "."
)

## Resolve full path for source
$sourcePath = Resolve-Path -Path $sourcePath

## Define VS Code settings directory
$destinationPath = "$env:APPDATA\Code\User"

## Define backup directory
$backupPath = "$sourcePath\backup"

## Ensure the destination exists
if (!(Test-Path -Path $destinationPath)) {
    Write-Host "VS Code settings folder not found at $destinationPath. Exiting..."
    exit 1
}

## Create a backup folder in the current location
if (!(Test-Path -Path $backupPath)) {
    New-Item -ItemType Directory -Path $backupPath | Out-Null
}

## Backup existing settings
$files = @("settings.json", "keybindings.json")
$snippetsPath = "$destinationPath\snippets"

foreach ($file in $files) {
    $filePath = "$destinationPath\$file"
    if (Test-Path -Path $filePath) {
        Copy-Item -Path $filePath -Destination $backupPath -Force
        Write-Host "Backed up $file to $backupPath"
    }
}

## Backup snippets if they exist
if (Test-Path -Path $snippetsPath) {
    Copy-Item -Path $snippetsPath -Destination $backupPath -Recurse -Force
    Write-Host "Backed up snippets to $backupPath"
}

## Restore settings from source path
foreach ($file in $files) {
    $filePath = "$sourcePath\$file"
    if (Test-Path -Path $filePath) {
        Copy-Item -Path $filePath -Destination $destinationPath -Force
        Write-Host "Restored $file from $sourcePath to $destinationPath"
    } else {
        Write-Host "$file not found in $sourcePath, skipping..."
    }
}

## Restore snippets if they exist
$sourceSnippetsPath = "$sourcePath\snippets"
if (Test-Path -Path $sourceSnippetsPath) {
    Copy-Item -Path $sourceSnippetsPath -Destination $destinationPath -Recurse -Force
    Write-Host "Restored snippets from $sourcePath to $destinationPath"
}

Write-Host "VS Code settings restore completed!"