Hide the Console Window When Auto-Starting WSL2
WSL2 automatically stops the instance when all terminal windows are closed. While you can mitigate this by setting vmIdleTimeout=-1 and instanceIdleTimeout=-1 in .wslconfig, some versions still terminate the instance. A reliable approach is to launch a persistent process at login to keep the instance alive, but running wsl.exe directly through Task Scheduler pops up a black console window. This post covers wrapping it in a VBS script to hide that window.
Create the VBS Launch Script
Create a VBS script file on Windows, for example C:\Users\YourUsername\start-wsl.vbs, with the following content:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "wsl.exe -d archlinux -u root /bin/bash -c ""sleep infinity""", 0, FalseReplace archlinux with your WSL distribution name (run wsl -l in PowerShell to check).
WshShell.Run parameter explanation:
- Second parameter
0— hide the window, no UI shown - Third parameter
False— do not wait for the child process to finish, the script exits immediately
Create the Scheduled Task
Run the following commands in PowerShell to create a scheduled task that runs at logon:
$action = New-ScheduledTaskAction -Execute "wscript.exe" -Argument '"C:\Users\YourUsername\start-wsl.vbs"'
$trigger = New-ScheduledTaskTrigger -AtLogOn
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit (New-TimeSpan -Seconds 0)
Register-ScheduledTask -TaskName "WSL2 AutoStart" -Action $action -Trigger $trigger -Settings $settingsVerification
Log off and back on (or reboot), then run wsl -l --running in PowerShell to confirm the WSL instance is running. No console window will appear during the process.