The Windows Subsystem for Linux (WSL) is a powerful tool for developers, allowing native Linux environments to run seamlessly within Windows. However, by default, WSL only starts when you manually launch a Linux terminal distribution or execute a WSL command. If you run background services inside WSL—such as web servers, Docker containers, or cron jobs—you will likely want the subsystem to start automatically as soon as Windows boots up.
Creating a VBScript to Start WSL Silently
To start WSL automatically without flashing a terminal window on your screen every time you log in, you can use a simple Visual Basic Script (VBScript). This script will execute a WSL command in the background.
- Press the Windows Key, type Notepad, and press Enter to open a new text document.
- Paste the following code into the Notepad window:
Set WshShell = CreateObject("WScript.Shell") WshShell.Run "wsl.exe ~ -e service cron start", 0, falseNote: This specific example starts the cron service inside your default WSL distribution. If you only want to initialize the WSL virtual machine without starting a specific service, you can change the command to
wsl.exe ~ -e true. The0parameter ensures the command window remains completely hidden. - Click File and select Save As.
- In the “Save as type” dropdown menu, select All Files (*.*).
- Name the file
start-wsl.vbsand save it to a location where you won’t accidentally delete it, such as your Documents folder.
Adding the Script to the Windows Startup Folder
Now that the script is created, you need to tell Windows to execute it automatically every time you log into your user account.
- Press the Windows Key + R to open the Run dialog box.
- Type
shell:startupand press Enter. This will open the hidden Windows Startup folder in File Explorer. - Locate the
start-wsl.vbsfile you created earlier. - Right-click the file and select Copy (or press
Ctrl+C). - Right-click inside the empty space of the Startup folder and select Paste shortcut. It is important to paste it as a shortcut rather than moving the original file, ensuring you can easily manage or edit the script later from its original location.
The next time you restart your computer and log in, Windows will execute the VBScript, silently initializing your WSL instance in the background.