Skip to content

Instantly share code, notes, and snippets.

@jcchikikomori
Created November 10, 2025 02:57
Show Gist options
  • Select an option

  • Save jcchikikomori/19e8dcfccf9f6eeff05a0ffb568294fe to your computer and use it in GitHub Desktop.

Select an option

Save jcchikikomori/19e8dcfccf9f6eeff05a0ffb568294fe to your computer and use it in GitHub Desktop.
WSL2 Import/Export tool.
# WSL2 Backup and Restore Script (No Compression)
# Features: WSL check, distro listing, folder picker, overwrite prompt, cleanup residuals
Add-Type -AssemblyName System.Windows.Forms
# Check if WSL is installed
$wslInstalled = Get-Command wsl.exe -ErrorAction SilentlyContinue
if (-not $wslInstalled) {
Write-Host "WSL is not installed. Please install it: https://learn.microsoft.com/en-us/windows/wsl/install"
exit
}
Write-Host "Choose an option:"
Write-Host "1. Backup WSL2 container"
Write-Host "2. Restore WSL2 container"
$choice = Read-Host "Enter choice (1 or 2)"
function List-Distros {
Write-Host "`nInstalled WSL Distributions:"
wsl -l
Write-Host ""
}
function Select-Folder {
$dialog = New-Object System.Windows.Forms.FolderBrowserDialog
$dialog.Description = "Select a folder to save or restore backup"
if ($dialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
return $dialog.SelectedPath
} else {
Write-Host "No folder selected. Exiting."
exit
}
}
function Cleanup-Temp {
Get-ChildItem $env:TEMP -Filter "*-backup.tar" -ErrorAction SilentlyContinue | Remove-Item -Force
Get-ChildItem $env:TEMP -Filter "*-restore.tar" -ErrorAction SilentlyContinue | Remove-Item -Force
}
if ($choice -eq "1") {
List-Distros
$distro = Read-Host "Enter WSL2 distribution name to backup"
$folderPath = Select-Folder
$fileName = Read-Host "Enter backup file name (no extension)"
$backupPath = Join-Path $folderPath "$fileName.tar"
if (Test-Path $backupPath) {
$overwrite = Read-Host "File already exists. Overwrite? (yes/no)"
if ($overwrite -ne "yes") {
Write-Host "Backup canceled."
exit
}
}
try {
Write-Host "Exporting WSL distro... This may take a while."
wsl --export $distro $backupPath
Cleanup-Temp
Write-Host "✅ Backup completed and saved as: $backupPath"
} catch {
Write-Host "❌ Error during backup: $($_.Exception.Message)"
Write-Host "See: https://learn.microsoft.com/en-us/windows/wsl/use-custom-distro"
}
}
elseif ($choice -eq "2") {
List-Distros
$distro = Read-Host "Enter WSL2 distribution name to restore"
$folderPath = Select-Folder
$fileName = Read-Host "Enter backup file name (no extension)"
$backupPath = Join-Path $folderPath "$fileName.tar"
if (-not (Test-Path $backupPath)) {
Write-Host "❌ Backup file not found at $backupPath"
exit
}
$exists = Read-Host "Does the target container already exist? (yes/no)"
try {
if ($exists -eq "yes") {
wsl --unregister $distro
Write-Host "Existing container removed."
}
Write-Host "Importing WSL distro... This may take a while."
wsl --import $distro "$env:LOCALAPPDATA\Packages\$distro" $backupPath
Cleanup-Temp
Write-Host "✅ Restore completed."
} catch {
Write-Host "❌ Error during restore: $($_.Exception.Message)"
Write-Host "See: https://github.com/microsoft/WSL/issues"
}
}
else {
Write-Host "Invalid choice. Please run the script again."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment