Skip to content

Instantly share code, notes, and snippets.

@elonzh
Last active November 13, 2023 16:45
Show Gist options
  • Select an option

  • Save elonzh/38d104e60fee8c638f935d049d5969c3 to your computer and use it in GitHub Desktop.

Select an option

Save elonzh/38d104e60fee8c638f935d049d5969c3 to your computer and use it in GitHub Desktop.

Revisions

  1. elonzh revised this gist Nov 13, 2023. No changes.
  2. elonzh created this gist Nov 13, 2023.
    68 changes: 68 additions & 0 deletions Move-WSL.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    <#
    .SYNOPSIS
    Migrate a specified WSL (Windows Subsystem for Linux) distribution to a new location.
    .DESCRIPTION
    The Move-WSL function terminates, exports, unregisters, and re-imports a specified WSL distribution to a new location. This is useful for moving the distribution to a different drive or directory.
    .PARAMETER Distro
    The name of the WSL distribution that you want to move. This parameter is mandatory.
    .PARAMETER Location
    The target directory where the WSL distribution will be moved. This parameter is mandatory.
    .EXAMPLE
    Move-WSL -Distro Ubuntu -Location D:\WSL
    Moves the 'Ubuntu' WSL distribution to the 'D:\WSL' directory.
    .OUTPUTS
    Outputs the path where the WSL distribution is exported.
    .NOTES
    Ensure that the specified location has enough space for the WSL distribution. The function might fail if there's insufficient space.
    .LINK
    For more information about WSL commands, visit: https://docs.microsoft.com/en-us/windows/wsl/reference
    Reference:
    - https://superuser.com/a/1732559/1114962
    - https://github.com/microsoft/WSL/issues/4482#issuecomment-1025130239
    #>

    function Move-WSL {
    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$true)]
    [string]$Distro,

    [Parameter(Mandatory=$true)]
    [string]$Location
    )

    try {
    # Terminate the specified WSL distribution
    wsl --terminate $Distro
    if ($LASTEXITCODE -ne 0) { throw "Failed to terminate WSL distribution: $Distro" }

    # Construct the file path
    $filePath = Join-Path $Location "$Distro.vhdx"

    # Export the WSL distribution
    wsl --export $Distro $filePath --vhd
    if ($LASTEXITCODE -ne 0) { throw "Failed to export WSL distribution: $Distro" }

    # Unregister the WSL distribution
    wsl --unregister $Distro
    if ($LASTEXITCODE -ne 0) { throw "Failed to unregister WSL distribution: $Distro" }

    # Import the WSL distribution in place
    wsl --import-in-place $Distro $filePath
    if ($LASTEXITCODE -ne 0) { throw "Failed to import WSL distribution: $Distro" }

    # Output the file path
    Write-Output "WSL distribution $Distro has been moved to $filePath"
    Write-Output "Moving distribution may change the default user. To use your own account, run 'ubuntu config --default-user <yourname>' or config the default user in /etc/wsl.conf according to https://learn.microsoft.com/en-us/windows/wsl/wsl-config#user-settings."
    }
    catch {
    Write-Error "An error occurred: $_"
    }
    }