Basic WSL Commands

Basic WSL Commands

WSL stands for windows subsystem for Linux. In WSL 2 Microsoft inserted an actual Linux kernel inside windows 10.

Run these commands in PowerShell or in the windows command line.

  • Run Linux commands from the Windows command line.

wsl ls -alh
# this will run the ls command in the current directory.

wsl sudo apt update
# this updates the package lists for upgrades 
# for packages that need upgrading.
  • Mix Linux and Windows commands

wsl ls -la | findstr "hello"
# this will use the ls command to list the contents of a dir 
# and then pipe the output to the findstr command in windows.

dir | wsl grep "hello"
  • Open Windows software from Linux

notepad.exe
# this will open notepad

notepad.exe "C:\folder\hello.txt"
# open a file in notepad

explorer.exe .
# this will open the explorer in the current directory.
  • Launch WSL from the windows command line

wsl 
# or
wsl.exe
# this will open WSL inside the current directory

exit 
# this will close WSL
  • List installed distributions

wsl --list
# this will list all the installed distributions

wsl --list --running
# this will list all the running distributions
  • Set a default distribution

wsl -s <DistributionName>, wsl --setdefault <DistributionName>

# exmaple 
# wsl -s Ubuntu
  • Unregister and reinstall a distribution

Unregister means uninstall here.
While the distribution can be installed from the store it can not be uninstalled from the store.


wsl --unregister <DistributionName>

To reinstall, find the distribution in the Microsoft store and select "Launch".

  • Set WSL 2 as your default version

wsl --set-default-version 2
  • Know which distribution is running what version of WSL

wsl --list --verbose
  • Set your distribution to use WSL 1 or WSL 2

wsl --set-version <distribution name> <versionNumber>

# example
# wsl --set-version Ubuntu 2
  • WSL 2 your default architecture

wsl --set-default-version 2

Did you find this article valuable?

Support Harshit Saini by becoming a sponsor. Any amount is appreciated!