Linux Shell Commands to Update Windows Subsystem for Linux

This blog post provides information about Linux shell commands that we can use to update Windows Subsystem for Linux and Linux itself.

We can use the wsl.exe command with the –update argument to update WSL, but we need to use an elevated command shell. For this we can use the runas.exe command, but we have to specify a user with administrative rights, and we should not open the shell if no updates are available.

wsl.exe --update 2>&1 | grep -iq 'No updates are available.'
if [ $? != 1 ]; then
  runas.exe /env /user:administrator "cmd.exe /k wsl.exe --update"

If wsl.exe indicates that updates are available, then this opens a new Windows console, runs the wsl.exe –update command, and leaves that console window open. Changing the /k to /c or just removing cmd.exe /c causes the console window to close itself, but then you cannot read its messages.

runas.exe /env /user:administrator "wsl.exe --update"

The runas.exe command prompts for the password of the local Windows administrator account. We can use an alternate account, such as the Windows account running the Linux shell, which we can get from the %USERNAME% Windows environment variable.

user=`cmd.exe /c echo %USERNAME% | sed -s 's/\r//g'` 
runas.exe /env /user:$user "cmd.exe /k wsl.exe --update"

After updating WSL, we can update the Linux distribution.

sudo apt update
sudo apt full-upgrade
sudo apt autoremove

The sudo (super user do) command prompts for the password of the current Linux user.

I guess we could see if any updates are available for Windows in the same process, possibly before updating WSL or even Linux, as Windows updates may include WSL updates.

explorer.exe ms-settings:windowsupdate-action

As no WSL or Linux updates are available for me, I have nothing to show, but we can do something similar with chkdsk.exe, which also requires privilege escalation.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: