Skip to content

Instantly share code, notes, and snippets.

@avisek
Last active August 23, 2026 07:41
Show Gist options
  • Select an option

  • Save avisek/aa9bf65d7a9e1657068a2e20c631f136 to your computer and use it in GitHub Desktop.

Select an option

Save avisek/aa9bf65d7a9e1657068a2e20c631f136 to your computer and use it in GitHub Desktop.
How I saved my WSL from a bricked Windows with one robocopy

WSL backup and restore guide

Your whole WSL distro is one file: ext4.vhdx. Copy that file, and you have everything. Home dir, installed packages, configs, all of it.

Where the vhdx lives

Store-installed distros:

C:\Users\<user>\AppData\Local\Packages\<DistroFolder>\LocalState\ext4.vhdx

<DistroFolder> looks like CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc.

Find it (cmd):

dir /s /b C:\Users\<user>\AppData\Local\Packages\ext4.vhdx

Find it (PowerShell):

Get-ChildItem C:\Users\<user>\AppData\Local -Recurse -Filter ext4.vhdx

Imported distros live wherever you imported them (for example C:\WSL\<name>). wsl -l -v lists all distros.

Note: AppData is hidden. Use dir /a to see it.

Backup (normal, Windows works)

Best method. Shut down WSL first, then export:

wsl --shutdown
wsl --export Ubuntu D:\Backup\ubuntu.tar

Or copy the vhdx directly after wsl --shutdown. Never copy it while the distro runs. You get a corrupt disk.

Backup (dead Windows, from installer cmd)

Boot the Windows installer USB. Press Shift+F10 at the install screen for cmd.

  1. Drive letters get reshuffled in this environment. Find your real Windows drive:
for %d in (C D E F G H I) do @dir %d:\Windows /b >nul 2>&1 && echo %d: has Windows
  1. Find the vhdx (use the letter from step 1, here D:):
dir /s /b D:\Users\<user>\AppData\Local\Packages\ext4.vhdx
  1. Copy its folder to your backup drive:
robocopy "D:\Users\<user>\AppData\Local\Packages\<DistroFolder>\LocalState" X:\Backup\WSL /E /R:1 /W:1
  1. Verify. Compare file sizes on both sides with dir before you wipe anything.

Bonus tricks for that environment:

  • No GUI? Run D:\Windows\notepad.exe, then File > Open, file type "All Files". The Open dialog is a mini file explorer. Right-click to copy and paste files. Do not double-click files.
  • PowerShell (has ls): D:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
  • List drives: wmic logicaldisk get name,volumename,size

Restore

On the fresh Windows, install the WSL engine (admin PowerShell):

wsl --install --no-distribution

Reboot. The import fails until you do.

From a tar export:

wsl --import Ubuntu C:\WSL\Ubuntu D:\Backup\ubuntu.tar

From a vhdx (copies it to the new location):

mkdir C:\WSL\Ubuntu
wsl --import Ubuntu C:\WSL\Ubuntu D:\Backup\WSL\ext4.vhdx --vhd

Or use the vhdx in place, no copy. Warning: the backup file becomes the live disk.

wsl --import-in-place Ubuntu D:\Backup\WSL\ext4.vhdx

Gotcha: if --import says path not found and your source path is correct, create the destination folder first with mkdir.

Fix the root login

Imported distros log you in as root. Fix it inside the distro:

printf '\n[user]\ndefault=<linuxuser>\n' >> /etc/wsl.conf

Then restart it:

wsl --terminate Ubuntu
wsl -d Ubuntu

whoami should show your user. Check ls /home if you forgot the name.

Multiple distros

wsl -l -v                     # list all, * marks default
wsl -d <name>                 # run a specific one
wsl --set-default <name>      # change what plain `wsl` opens
wsl --install Ubuntu          # fresh distro alongside the old one
wsl --unregister <name>       # DELETE a distro and its disk, no undo

Old and fresh distros coexist fine. Name imports something like Ubuntu-old to keep them apart.

Rules that save you

  • Never copy a vhdx while WSL runs. wsl --shutdown first.
  • Verify the backup size before you wipe.
  • Keep the backup until the restored distro proves itself for a week or two.
  • wsl --export beats raw vhdx copies for planned backups. Do one now and then.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment