Skip to content

Latest commit

 

History

History
236 lines (177 loc) · 5.76 KB

File metadata and controls

236 lines (177 loc) · 5.76 KB

WSL Installation Script - Detailed Code Explanation

This script uses a mix of:

  • PowerShell native cmdlets
  • Windows system executables
  • Environment variables
  • String interpolation
  • Conditional logic
  • Pipeline operations
  • Error handling

Now part by part it can broken down into these followin procedures:

1. Administrator Check

$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
    Write-Host "Please run this script as Administrator" -ForegroundColor Red
    Exit 1
}

Explanation:

  • [Security.Principal.WindowsPrincipal] - .NET class for user security context
  • [Security.Principal.WindowsIdentity]::GetCurrent() - Gets current user's identity
  • IsInRole() - Checks if user has specific security role
  • Write-Host - Outputs text to console with color formatting
  • Exit 1 - Terminates script with error code 1

2. Error Handling Function

function Handle-Error {
    param($ErrorMessage)
    Write-Host "Error: $ErrorMessage" -ForegroundColor Red
    Write-Host "Press any key to continue..."
    $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
}

Breakdown:

  • function - Declares a reusable code block
  • param() - Defines function parameters
  • $Host.UI.RawUI.ReadKey() - Waits for user keypress
  • 'NoEcho,IncludeKeyDown' - Keypress options
  • $null = - Suppresses unwanted output

3. Temporary Directory Setup

$tempDir = "$env:USERPROFILE\WSLSetup"
New-Item -ItemType Directory -Force -Path $tempDir | Out-Null

Details:

  • $env:USERPROFILE - User profile directory path
  • New-Item - Creates new filesystem item
  • -Force - Overwrites existing items
  • | Out-Null - Suppresses console output

4. WSL Cleanup Process

Write-Host "=== Starting WSL Cleanup ===" -ForegroundColor Cyan
Get-Process | Where-Object { $_.Name -like "*wsl*" } | Stop-Process -Force -ErrorAction SilentlyContinue

Components:

  • Get-Process - Lists running processes
  • Where-Object - Filters process list
  • -like "*wsl*" - Pattern matching
  • Stop-Process -Force - Terminates processes
  • -ErrorAction SilentlyContinue - Ignores errors

5. Registry Cleanup

$registryPath = "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss"
if (Test-Path $registryPath) {
    Remove-Item -Path $registryPath -Recurse -Force
}

Analysis:

  • HKCU: - Current user registry hive
  • Test-Path - Checks existence
  • Remove-Item -Recurse - Recursive deletion
  • -Force - Bypasses confirmation

6. WSL Folder Cleanup

$foldersToRemove = @(
    "$env:USERPROFILE\AppData\Local\Packages\*WSL*",
    "$env:USERPROFILE\AppData\Local\Microsoft\WindowsApps\Ubuntu*",
    "$env:LOCALAPPDATA\Packages\*Ubuntu*",
    "$env:LOCALAPPDATA\Packages\*WSL*"
)

Structure:

  • @() - Array constructor
  • Environment variables:
    • $env:USERPROFILE - User directory
    • $env:LOCALAPPDATA - Local AppData
  • Wildcard patterns (*) for flexible matching

7. WSL Installation Process

$wslUpdateUrl = "https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi"
$wslUpdateFile = "$tempDir\wsl_update_x64.msi"
Invoke-WebRequest -Uri $wslUpdateUrl -OutFile $wslUpdateFile

Components:

  • Invoke-WebRequest - Downloads files
  • -Uri - Source URL
  • -OutFile - Destination path

8. Installation Verification

$installProcess = Start-Process msiexec.exe -ArgumentList "/i `"$wslUpdateFile`" /quiet" -Wait -PassThru
if ($installProcess.ExitCode -ne 0) {
    throw "WSL Update Package installation failed"
}

Details:

  • Start-Process - Executes process
  • -ArgumentList - Command parameters
  • -Wait - Prevents continuation until complete
  • -PassThru - Returns process object
  • ExitCode - Process completion status

9. Windows Features Management

$features = @(
    "Microsoft-Windows-Subsystem-Linux",
    "VirtualMachinePlatform"
)

foreach ($feature in $features) {
    Write-Host "Enabling $feature..." -ForegroundColor Yellow
    $result = dism.exe /online /enable-feature /featurename:$feature /all /norestart
    if ($LASTEXITCODE -ne 0) {
        throw "Failed to enable $feature"
    }
}

Breakdown:

  • Array of required features
  • foreach loop for iteration
  • dism.exe - Deployment Image Servicing and Management
  • /online - Live system modification
  • $LASTEXITCODE - Command success check

10. Cleanup and Completion

Remove-Item -Path $wslUpdateFile -Force -ErrorAction SilentlyContinue
Remove-Item -Path $tempDir -Force -Recurse -ErrorAction SilentlyContinue

Write-Host "`n=== Installation Completed Successfully! ===" -ForegroundColor Green

Elements:

  • File cleanup
  • Directory removal
  • Success notification
  • Color-coded output

11. System Restart Handling

$restart = Read-Host "Would you like to restart now? (y/n)"
if ($restart -eq 'y') {
    Restart-Computer -Force
}

Components:

  • Read-Host - User input capture
  • Restart-Computer - System restart
  • -Force - Bypasses confirmations

Error Handling Structure

try {
    # Main installation code
} catch {
    Handle-Error $_.Exception.Message
    # Cleanup code
}

Framework:

  • try-catch block for error management
  • $_.Exception.Message - Error details
  • Custom error handler
  • Cleanup on failure


Special Syntax Elements Used:

  • $ - Variable identifier
  • "" - String with variable interpolation
  • # - Comment marker
  • { } - Code block delimiter
  • - - Parameter prefix
  • / - Command-line switch prefix
  • \ - Path separator
  • . - Method/property accessor