Skip to content

Instantly share code, notes, and snippets.

@kaic
Last active December 9, 2025 22:54
Show Gist options
  • Select an option

  • Save kaic/4eb9679f0130be9d5147e7e90dce8375 to your computer and use it in GitHub Desktop.

Select an option

Save kaic/4eb9679f0130be9d5147e7e90dce8375 to your computer and use it in GitHub Desktop.
This PowerShell script automates the setup of Windows Subsystem for Linux (WSL) with Ubuntu 20.04. It enables the required system features, sets WSL 2 as the default version, installs Ubuntu 20.04 via Winget if not already present, and configures it as the default distribution. Ideal for post-format automation and developer workstation setup.

configure_wsl.ps1

This PowerShell script automates the setup of Windows Subsystem for Linux (WSL) with Ubuntu 20.04.

🧩 What it does

  • Enables WSL and Virtual Machine Platform features.
  • Sets WSL 2 as the default version.
  • Installs Ubuntu 20.04 LTS using Winget (if not already installed).
  • Sets Ubuntu 20.04 as the default WSL distribution.

βš™οΈ Prerequisites

  • Windows 10 (version 2004 or later) or Windows 11
  • PowerShell running as Administrator
  • Internet connection
  • Winget installed (comes pre-installed on modern Windows)

πŸš€ How to use

  1. Download the configure_wsl.ps1 script.
  2. Open PowerShell as Administrator.
  3. Run the script:
powershell -ExecutionPolicy Bypass -File configure_wsl.ps1

πŸ“ Recommended structure

You can include this script as part of a full system automation process (e.g., with a .bat file for reinstalling apps and restoring configs).

βœ… License

MIT License β€” feel free to use and modify.

# Script: configure_wsl.ps1
# Purpose: Automates basic WSL setup with Ubuntu
Write-Host "Starting WSL configuration..."
# Enable required features
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
dism.exe /online /enable-feature /featurename:Microsoft-Hyper-V-All /all /norestart
dism.exe /online /enable-feature /featurename:HypervisorPlatform /all /norestart
# Base WSL setup (no distro)
wsl.exe --install --no-distribution
# Set WSL 2 as the default version
wsl --set-default-version 2
# Install Ubuntu 20.04 if not installed
Write-Host "Checking Ubuntu 20.04 installation..."
$ubuntuInstalled = wsl --list --online | Select-String "Ubuntu-20.04"
if (-not $ubuntuInstalled) {
Write-Host "Installing Ubuntu 20.04 via winget..."
winget install --id=Canonical.Ubuntu.2004 -e --silent
} else {
Write-Host "Ubuntu 20.04 already listed as available online."
}
# Wait a moment to ensure registration
Start-Sleep -Seconds 3
# List installed distros and set the first Ubuntu distro found as default
$distrosRaw = wsl --list --quiet
$distros = $distrosRaw | ForEach-Object { $_.Trim() } | Where-Object { $_ -like "*Ubuntu*" }
if ($distros.Count -gt 0) {
$defaultDistro = $distros[0]
Write-Host "Setting '$defaultDistro' as the default WSL distro..."
wsl --set-default "$defaultDistro"
} else {
Write-Host "No Ubuntu distribution found to set as default."
Write-Host "If installation failed with error 0x80370114, ensure virtualization is enabled in your BIOS."
Write-Host "Instructions: https://aka.ms/enablevirtualization"
}
Write-Host "WSL configuration completed!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment