The Founder’s Guide:

How to Run WSL2 at Startup on Windows

The expanded version with explanations and screenshots

David Littlefield
The Startup

--

Image by Bram Van Oost

“The condensed version of this article uses copy and paste code to help you get the outcome ASAP ⚡”

Open PowerShell:

PowerShell is a command-line shell and object-oriented scripting language that’s used to automate administrative tasks and configure system settings It can be used to automate practically anything in the operating system. It also replaced Command Prompt as the default system shell for Windows 10.

  1. Press “⊞ Windows”
  2. Enter “PowerShell” into the search bar
  3. Click “Run as Administrator”

Create the Visual Basic Script File:

The Echo command is used to print text it receives from the argument to the screen or to a computer file. It can be used in shell scripts and batch files to print the output of other commands or as part of other commands to insert text. It can also overwrite the text in an existing file or append it at the end.

  1. Copy the command from below these instructions
  2. Paste the command into PowerShell
  3. Press “Enter”
echo "" > $HOME\run_wsl2_at_startup.vbs

Open the Visual Basic Script File:

The Visual Basic Script File (vbs) is a script file that contains source code to be executed with a Windows Scripting Host program. It can be run using Wscript which is intended for interactive window programs. It can also be ran using Cscript which is intended for non-interactive console programs.

  1. Copy the command from below these instructions
  2. Paste the command into PowerShell
  3. Press “Enter”
notepad $HOME\run_wsl2_at_startup.vbs

Edit the Visual Basic Script File:

Task Scheduler can’t start WSL2 in the background as expected at startup because background processes are run in session zero. It causes programs that run from the command-line to start without a window. It can also be easily fixed with a visual basic script that controls how the process starts.

  1. Find the Ubuntu version from below these instructions
  2. Copy the provided code
  3. Paste the code into Notepad
  4. Click the “File” menu
  5. Click “Save”
Ubuntu 20.04:
set object = createobject("wscript.shell")
object.run "wsl.exe --distribution Ubuntu-20.04", 0
Ubuntu 18.04:
set object = createobject("wscript.shell")
object.run "wsl.exe --distribution Ubuntu-18.04", 0

Open Task Scheduler:

Task Scheduler is a program that automatically performs tasks when certain criteria are met. It can start programs, execute commands, and run scripts at specified times or after specified time intervals and events. It can also be programmed using the graphical user interface or command-line interface.

  1. Press “⊞ Windows”
  2. Enter “Task Scheduler” into the search bar
  3. Click “Task Scheduler”

Create the Task:

The Task is the work the Task Scheduler performs according to the action, trigger, principal, and settings that are specified by the user. It refers to the action that’s taken using the principal account in response to the trigger. It also includes the action to take in the circumstances listed in the settings.

  1. Click “Create Basic Task” in the right panel
  2. Copy the text from below these instructions
  3. Paste the text into the “Name” text field
  4. Click “Next”
  5. Select “When the Computer Starts”
  6. Click “Next”
  7. Select “Start a Program”
  8. Click “Next”
Windows Subsystem for Linux 2 (WSL2)

Specify the Action:

The Action is used in Task Scheduler to specify the work that’s performed when a task is triggered. I can launch programs, execute commands, run scripts, and open files. It can also perform a single action and multiple actions which can include up to 32 actions that are executed sequentially.

  1. Copy the path from below these instructions
  2. Paste the path into the “Program/Script” text field
  3. Click “Next”
  4. Click “Yes”
  5. Check “Open the Properties Dialog…”
  6. Click “Finish”
%USERPROFILE%\run_wsl2_at_startup.vbs

Specify the Trigger:

The Trigger is used in Task Scheduler to set the conditions that determine whether a task is performed. It can start a task using time-based triggers and event-based triggers. It can also start a task using a single trigger and multiple triggers which can include up to 48 triggers that all start the task.

  1. Click “Triggers” in the top tab bar
  2. Click “At Startup”
  3. Click “Edit”
  4. Check “Delay Task For”
  5. Enter “30 Seconds”
  6. Click “OK”

Run in the Background:

The Run Whether User Is Logged in or Not option is used in Task Scheduler to run a task even if the user is logged out. It runs the task in the background without a window which can’t be used interactively. It also causes processes that are started by the task to operate in the background without a window.

  1. Click “General” in the tab bar
  2. Select “Run Whether User Is Logged On or Not”
  3. Check “Run With Highest Privileges”

Remove the Time Limit:

The Stop the Task if It Runs Longer Than setting is used in Task Scheduler to limit the length of time that a task is able to run. It stops the task after the specified time limit is reached which prevents the task from taking too long to execute. It also prevents a frozen task from potentially running forever.

  1. Click “Settings” in the tab bar
  2. Uncheck “Stop the Task if It Runs Longer Than”
  3. Click “OK”
  4. Restart the computer

Open PowerShell:

PowerShell is a command-line shell and object-oriented scripting language that’s used to automate administrative tasks and configure system settings It can be used to automate practically anything in the operating system. It also replaced Command Prompt as the default system shell for Windows 10.

  1. Press “⊞ Windows”
  2. Enter “PowerShell” into the search bar
  3. Click “Run as Administrator”

Get Running Processes:

The Get-Process command is used in PowerShell to access all the processes that are currently running on the computer. It displays every process that’s running by default. It also specifies a process name or process ID to display the specific process that’s running which can be piped to other commands.

  1. Copy the command from below these instructions
  2. Paste the command into PowerShell
  3. Press “Enter”
get-process

Bonus: Run a Program At Startup:

Task Scheduler can’t start WSL2 in the background as expected at startup because background processes are run in session zero. It causes programs that run from the command-line to start without a window. It can also be easily fixed with a visual basic script that controls how the process starts.

  1. Find the Ubuntu version from below these instructions
  2. Replace the “Program” with the program name
  3. Replace the “Arguments” with the arguments
  4. Copy the provided code
  5. Paste the code into Notebook
  6. Click the “File” menu
  7. Click “Save”
Ubuntu 20.04:
set object = createobject("wscript.shell")
object.run "wsl.exe --distribution Ubuntu-20.04 program arguments", 0
Ubuntu 18.04:
set object = createobject("wscript.shell")
object.run "wsl.exe --distribution Ubuntu-18.04 program arguments", 0

“Hopefully, this article helped you get the 👯‍♀️🏆👯‍♀️, remember to subscribe to get more content 🏅”

Next Steps: AI, Machine Learning, and Deep Learning

This article is part of a mini-series that helps readers set up everything they need to start using WSL2 for artificial intelligence, machine learning, deep learning, and or data science. It includes articles that contain instructions with copy and paste code and screenshots that help readers get the outcome as soon as possible. It also includes articles that contain instructions with explanations and screenshots that help readers process what’s happening.

01. Install Windows Subsystem for Linux 2 (WSL2)
02. Install the NVIDIA CUDA Driver and Toolkit in WSL2
03. Install Software From Source Code in WSL2
04. Install the Jupyter Notebook Home and Public Server in WSL2
05. Install Virtual Environments in Jupyter Notebook in WSL2
06. Install Programs With a Graphical User Interface in WSL2
07. Install Ubuntu Desktop With a Graphical User Interface in WSL2

Glossary:

The Shell is an interpreter that presents the command-line interface to users and allows them to interact with the kernel. It lets them control the system using commands entered from a keyboard. It also translates the commands from the programming language into the machine language for the kernel.
[Return]

The Interpreter is a program that reads through instructions that are written in human-readable programming languages and executes the instructions from top to bottom. It translates each instruction to a machine language the hardware can understand, executes it, and proceeds to the next instruction.[Return]

The Command-Line Interface (CLI) is a program that accepts text input from the user to run commands on the operating system. It lets them configure the system, install software, and access features that aren’t available in the graphical user interface. It also gets referred to as the terminal or console.[Return]

The Kernel is the program at the heart of the operating system that controls everything in the computer. It facilitates the memory management, process management, disk management, and task management. It also facilitates communication between the programs and hardware in machine language.[Return]

Visual Basic Script (VBScript) is a scripting language that’s based on Visual Basic. It was created for developing web pages but it has become a popular language for writing batch files on Windows. It can also be executed using Windows Scripting Host to interact with the Windows operating system.
[Return]

Create Object is a function in Visual Basic Script that creates an instance of a Component Object Model object. It specifies the programmatic identifier to access the methods and properties in the component object model. This lets users launch and control external programs that have a program identifier.
[Return]

The Instance refers to the distinct identity of a particular object that exists among zero or more objects that belong to the same class. It has the same attributes as the other objects but the attribute values are usually different. It can also have the same values but it would still be a different instance.[Return]

The Component Object Model (COM) is an object model and programming standard that allows objects to interact with other objects using different programming languages and operating systems. It defines one or more sets of related functions that can be used to start and control other programs.[Return]

The WScript object is used in Visual Basic Script to create an instance of Windows Script Host. It runs scripts in multiple programming languages that use object models to perform tasks. It also has access to programs, shortcuts, environment variables, the registry, and the operating system.
[Return]

The WshShell object is used in Visual Basic Script to allow users to interact with the Windows operating system. It has access to environment variables, system folders, and the registry. It can also create shortcuts, display pop-up windows, and run programs with command-line arguments and keystrokes.[Return]

The Run method is used in Visual Basic Script to run programs. It can run graphical programs, command-line programs, and scripts using command-line arguments and keystrokes. It can also run programs with or without a window, activate program windows, and pause programs temporarily.[Return]

--

--

David Littlefield
The Startup

From: Non-Technical | To: Technical Founder | Writes: To Make It Easier For Everyone | Topics: #Startups #How-To #Coding #AI #Machine Learning #Deep Learning