# WSL2 Environment Check Script # This script checks WSL2 installation status on Windows # Set error action $ErrorActionPreference = 'Stop' # Color definitions $Red = [System.ConsoleColor]::Red $Green = [System.ConsoleColor]::Green $Yellow = [System.ConsoleColor]::Yellow # Logging functions function Write-Info { param([string]$Message) Write-Host '[INFO] ' -NoNewline -ForegroundColor $Green Write-Host $Message } function Write-Warn { param([string]$Message) Write-Host '[WARN] ' -NoNewline -ForegroundColor $Yellow Write-Host $Message } function Write-Error { param([string]$Message) Write-Host '[ERROR] ' -NoNewline -ForegroundColor $Red Write-Host $Message } function Wait-KeyPress { Write-Host "`nPress any key to exit..." -NoNewline $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown') Write-Host '' } # Check administrator privileges if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { Write-Warn 'Administrator privileges required' Write-Info 'Please right-click this script and select "Run as Administrator"' Wait-KeyPress exit 1 } Write-Info "`nStarting WSL2 environment check...`n" # Check Windows version Write-Info 'Checking Windows version...' $windowsVersion = (Get-WmiObject Win32_OperatingSystem).Caption Write-Host "Windows Version: $windowsVersion" # Check WSL installation status Write-Info 'Checking WSL installation...' try { $wslVersion = wsl --version Write-Info 'WSL is installed' Write-Host "`n$wslVersion`n" } catch { Write-Error 'WSL is not installed' Write-Info 'Please visit https://aka.ms/wsl to install WSL' Wait-KeyPress exit 1 } finally { # Clear errors $error.clear() } # Check WSL2 status Write-Info 'Checking WSL2 status...' try { $wslList = wsl -l -v if ($wslList -match '2') { Write-Info 'WSL2 is enabled' } else { Write-Warn 'WSL2 is not enabled' Write-Info 'WSL distribution list:' Write-Host "`n$wslList`n" Write-Info 'Please run the following command to enable WSL2:' Write-Host 'wsl --set-default-version 2' Wait-KeyPress exit 1 } } catch { Write-Error "Failed to get WSL status: $($_.Exception.Message)" Wait-KeyPress exit 1 } finally { # Clear errors $error.clear() } # Check Debian installation status Write-Info 'Checking Debian installation...' try { wsl -d Debian echo 'Debian is installed' | Out-Null Write-Info 'Debian is installed' } catch { Write-Warn 'Debian is not installed' Write-Info 'Please run the following command to install Debian:' Write-Host 'wsl --install -d Debian' Wait-KeyPress exit 1 } finally { # Clear errors $error.clear() } # Check virtualization status Write-Info 'Checking virtualization status...' try { $systemInfo = systeminfo if ($systemInfo -match 'Virtual Ethernet Adapter' -or $systemInfo -match 'hypervisor has been detected') { Write-Info 'Virtualization is enabled' } else { Write-Error 'Virtualization is not enabled' Write-Info 'Please enable virtualization (Intel VT-x or AMD-V) in BIOS' Wait-KeyPress exit 1 } } catch { Write-Error "Failed to check virtualization status: $($_.Exception.Message)" Wait-KeyPress exit 1 } finally { # Clear errors $error.clear() } # Check Windows features Write-Info 'Checking Windows features...' # Check Linux subsystem try { $wslFeature = Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux if ($wslFeature.State -eq 'Enabled') { Write-Info 'Linux subsystem is enabled' } else { Write-Warn 'Linux subsystem is not enabled' Write-Info 'Please run the following command to enable Linux subsystem:' Write-Host 'dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart' Wait-KeyPress exit 1 } } catch { Write-Error "Failed to check Linux subsystem status: $($_.Exception.Message)" Wait-KeyPress exit 1 } finally { # Clear errors $error.clear() } # Check virtual machine platform try { $vmPlatform = Get-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform if ($vmPlatform.State -eq 'Enabled') { Write-Info 'Virtual Machine Platform is enabled' } else { Write-Warn 'Virtual Machine Platform is not enabled' Write-Info 'Please run the following command to enable Virtual Machine Platform:' Write-Host 'dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart' Wait-KeyPress exit 1 } } catch { Write-Error "Failed to check Virtual Machine Platform status: $($_.Exception.Message)" Wait-KeyPress exit 1 } finally { # Clear errors $error.clear() } Write-Host "`n============================================" Write-Host " Check Complete! " Write-Host "============================================" Write-Host "`nYour WSL2 environment is ready:`n" Write-Info '1. Windows version is compatible' Write-Info '2. WSL2 is installed and enabled' Write-Info '3. Debian distribution is installed' Write-Info '4. Virtualization is enabled' Write-Info '5. Required Windows features are enabled' Write-Info 'You can now start using WSL2!' Write-Host "============================================" # Wait for user input before exit Wait-KeyPress