Just a quick note, mostly for myself, on how to move a WSL2 VHDX image to an alternate location.

Previously, I already touched on the subject of backing up WLS images. However, now I am looking to move my default Ubuntu WSL2 image to a detachable drive because my laptop’s internal SSD is running on space.

There is a proper way of doing this, and then there’s also the quick way. First, the proper way:

# Use PowerShell for this
# Identify the WSL image you wish to relocate
# In my case, the image I want to move is called "Ubuntu"
wsl --list --verbose

# Stop WSL processes
wsl --shutdown

# Create the target folder and export the image
# This may take a while
mkdir D:\wsl_ubuntu_001
wsl --export "Ubuntu" D:\wsl_ubuntu_001\ubuntu.tar

# Create the target instance folder and import the image
# This may also take a while
mkdir D:\WSL\instances\Ubuntu001
wsl --import "Ubuntu001" D:\WSL\instances\Ubuntu001 D:\wsl_ubuntu_001\ubuntu.tar

# Set the new instance as the default
wsl --set-default "Ubuntu001"

# Optionally, unregister the previous instance
wsl --unregister Ubuntu

# Now you can clean things up by removing the *tar file and
# removing the original *vhdx file from 
# C:\Users\<username>\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_<ID>\LocalState

The much faster way is to a) identify the instance name, b) shutdown WSL (see above), c) copy the *.vhdx file from the drive C: location to the new location, d) edit the registry key to point to the new target folder:

Open the registry editor as Admin and navigate to Computer\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss.

Identify the correct distro ID. The default distro ID is shown in the DefaultDistribution key. Go one level down into the correct distro ID and update the BasePath key to reflect the new location of the *.vhdx file.

Now you can launch your WSL2 instance via the usual shortcut. You will notice that the *.vhdx file in the new location now has the current timestamp, while the old image file is older. You should be safe to delete the old file and reclaim disk space.