Last active
December 27, 2023 21:32
-
-
Save nrclark/d19cdad7711d7471217d13af267b6b9a to your computer and use it in GitHub Desktop.
Powershell script to make WSL start at boot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ErrorActionPreference = "Stop" | |
$principal = aNew-ScheduledTaskPrincipal ` | |
-UserId "$env:USERDOMAIN\$env:USERNAME" ` | |
-LogonType S4U ` | |
-RunLevel Limited ` | |
-ThrottleLimit 1 | |
$settings = New-ScheduledTaskSettingsSet ` | |
-MultipleInstances IgnoreNew ` | |
-AllowStartIfOnBatteries ` | |
-DontStopIfGoingOnBatteries ` | |
-StartWhenAvailable ` | |
-DontStopOnIdleEnd ` | |
-ExecutionTimeLimit (New-TimeSpan -Hours 1) ` | |
-Priority 7 ` | |
-RestartInterval (New-TimeSpan -Minutes 5) ` | |
-RestartCount 3 | |
$trigger = New-ScheduledTaskTrigger -AtStartup | |
$distro=(wsl --list | ForEach-Object { ($_ -replace "`0", "").trim() } | | |
where {$_ -match "(?i)[(]default[)]$"} | | |
ForEach-Object {$_ -replace "(?i)[ (]default[)]$", ""}) | |
if ([string]::IsNullOrEmpty($distro)) { | |
Write-Error "No WSL default distro was detected." | |
} | |
$action = New-ScheduledTaskAction ` | |
-ThrottleLimit 1 ` | |
-Execute "wsl.exe" ` | |
-Argument "--distribution $distro sleep 0" ` | |
-Id "Launch WSL by running a no-op command" | |
$result = Register-ScheduledTask ` | |
-Force ` | |
-TaskName "StartWSL" ` | |
-Description "Start WSL on system boot" ` | |
-Trigger $trigger ` | |
-Principal $principal ` | |
-Action $action ` | |
-Settings $settings | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment