Auto-update software in WSL
Publish date: 3. May 2022
Last updated: 2. December 2022
Last updated: 2. December 2022
Windows Subsystem for Linux (WSL) allows running one or multiple Linux distributions on Windows. This article describes how to keep them updated with the latest software and security patches using Windos Task Scheduler.
Open PowerShell (no need to run it as Administrator) and run the following commands to create a scheduled task that runs every Monday at 12:00 and updates the default WSL distribution.
$Time = New-ScheduledTaskTrigger -At 12:00 -Weekly -WeeksInterval 1 -DaysOfWeek Monday
$Actions = @(
New-ScheduledTaskAction -Execute "wsl" -Argument "--user root --exec apt-get update"
New-ScheduledTaskAction -Execute "wsl" -Argument "--user root --exec apt-get upgrade --yes"
New-ScheduledTaskAction -Execute "wsl" -Argument "--user root --exec apt-get autoremove --yes"
)
$Settings = New-ScheduledTaskSettingsSet -WakeToRun:$false `
-MultipleInstances IgnoreNew `
-RunOnlyIfNetworkAvailable:$true `
-StartWhenAvailable:$true
Register-ScheduledTask -TaskName "Update wsl" -Trigger $Time -Action $Actions -Settings $Settings -TaskPath Updates
This example assumes we are working with an Ubuntu/Debian-based distribution.
Note: If you have multiple WSL distributions, you must duplicate the previous commands for each distro and add --distribution <ditro_name>