Unable to start WSL via Task Scheduler
Version
22000.978
WSL Version
- [X] WSL 2
- [ ] WSL 1
Kernel Version
5.15.57.1
Distro Version
Ubuntu 22.04
Other Software
No response
Repro Steps
Create a powershell script with the following:
# start SSH server and other applications in Ubuntu
wsl -d Ubuntu-22.04 -u root service ssh start
Start-Sleep -s 5
# add port forwarding rule (from WSL to host) for port 4242
$wsl_ip = (wsl hostname -I).trim()
Write-Host "WSL Machine IP: ""$wsl_ip"""
netsh interface portproxy add v4tov4 listenport=4242 connectport=4242 connectaddress=$wsl_ip
Create a Task Scheduler task to run the powershell script on boot.
- Run whether use is logged in or not
- Run with highest privileges
- Trigger: at startup (30 second delay)
- Action: start a program:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe - Action: Add arguments:
-ExecutionPolicy Bypass -File C:\Scripts\startwsl.ps1 - Settings: Allow task to be run on demand
Restart the machine, or manually run the task.
Expected Behavior
Upon reboot or manually running the task, the WSL instance should start and the ssh daemon should be running. This worked in previous versions of WSL (on Windows 10).
Actual Behavior
Either on reboot or on manually executing the task, WSL does not start.
C:\Windows\system32>wsl --list --running
There are no running distributions.
If I manually run the following command via the command prompt, WSL and the ssh daemon start properly.
C:\Windows\system32>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File C:\Scripts\startwsl.ps1
* Starting OpenBSD Secure Shell server sshd [ OK ]
Starting seafile daemon ...
Started: seafile daemon ...
WSL Machine IP: "172.21.82.221"
C:\Windows\system32>wsl --list --running
Windows Subsystem for Linux Distributions:
Ubuntu-22.04 (Default)
In summary, running the same powershell script to launch WSL works from the command prompt but does not work from the task scheduler.
Diagnostic Logs
No response
/logs
Hello! Could you please provide more logs to help us better diagnose your issue?
To collect WSL logs, download and execute collect-wsl-logs.ps1 in an administrative powershell prompt:
Invoke-WebRequest -UseBasicParsing "https://raw.githubusercontent.com/microsoft/WSL/master/diagnostics/collect-wsl-logs.ps1" -OutFile collect-wsl-logs.ps1
Set-ExecutionPolicy Bypass -Scope Process -Force
.\collect-wsl-logs.ps1
The scipt will output the path of the log file once done.
Once completed please upload the output files to this Github issue.
Click here for more info on logging
Thank you!
I have attached two logs. _powershell.zip when I ran my powershell script via the command line to start WSL (success), and _taskscheduler.zip when I ran the powershell script via the task scheduler (failure).
WslLogs-2022-09-21_20-52-49_powershell.zip
WslLogs-2022-09-21_20-54-39_taskscheduler.zip
I have the same problem. This problem seems to be specific to the Microsoft Store version of wsl. If I uninstall the Microsoft Store WSL app, task schedule can properly start wsl. I tried the vbs trick mentioned here without success. This problem seems to be related to the "Unable to start wsl.exe from a Session 0 prompt which users can run into when trying to start WSL from an ssh session" limitation mentioned in the release blogpost.
I can confirm, as @cheng-chi said, that this issue is only apparent in the Microsoft Store version of WSL. I am able to start WSL via the Task Scheduler when I use the non-Microsoft Store version.
And since I'd like to use WSLg, I need the Microsoft Store version...
+1
I have this issue too. Same deal - was working before. Then upgraded Windows to 22H2 & was prompted by WSL to run some WSL Update command for WSL to do with store (sadly I can't recall the exact command or find it - it was something that appeared at the first opening of a WSL terminal post-Windows update) - now Task Scheduler won't run my command anymore. Before that - it worked. Even if I open Task Scheduler, select the task, & click 'Run' - nothing happens.
Same as the above poster, it works when I run exactly the same command that I have in Task Scheduler, directly from UN-elevated CMD or UN-elevated Powershell.
CMD_WslLogs-2022-10-08_10-52-39.zip
Task_Scheduler_WslLogs-2022-10-08_10-50-45.zip
At time of posting this - it's 10:55AM AWST on 08 October 2022 - it shows the last run time (even though I've been clicking RUN) as two days ago (06 October 2022) - which is when I installed the new Windows update.

I think I have found a solution. According to this StackOverflow answer, we can escalate a process out of session 0 using psexec.exe.
- Download PsTools and extract PsExec.exe to a convenient location.
- Replace your command running wsl with
C:\<your psexec install path>\PsExec.exe -i 1 <your original command>
If the workaround works, it will be very useful until MS can resolve the issue. I'll give it a test & report back. Many thanks
Sadly the workaround didn't work. Even deleted the task & recreated it from scratch using the workaround, no change. Must be something to do with the Windows update.
@brettus78 I have created a GitHub repo for my solution of running wsl at startup. I have also installed the Windows 11 22H2 update so I think it shouldn't be the issue.
I have the same issue. If I try to use the psexec64.exe -i 1 <command> workaround, I keep getting a poweshell.exe exited with error code 0 error.
Been flat out - will try the example in the GitHub repo you posted, ASAP. But might be a couple of days before I get a chance.
~~Just happy to report that using psexec64.exe worked for me. I didn't need to port forward SSH though, so I didn't test that part.~~
~~@sikhness, I actually got the same message when I ran the psexec64.exe command in PowerShell. If my memory serves me correctly, WSL 2 should be running in the background by then.~~
Log for October 15, 2022: I just found that it didn't actually work, after shutting down my system when I went to bed and opening it again hours after I woke up. Tried Distrod, but to no avail. I'm ditching WSL 2 for the moment, until they support having one instance usable by all users.
I'll need to do further testing to isolate - but I recently received another Windows update (around features like tabs in File Explorer). As a test triggered by SeanBallais' comment above "having one instance usable by all users" - I thought I should try a Task Scheduler task that only runs when I'm logged in.
I tried it & it works.
Not sure if because I chose to only run once logged in - or if resolved by the Windows update. Suspect the former.
At some point, I'll try again with running without having logged in & see if it works since the update, but for now, it's working, so it's a low priority for me.

I can confirm that it works if you run the script with "only when user is logged on" and triggered at log on.
I need to use "Run whether user is logged in or not" to start wsl2 from taskScheduler. Is this working for anyone? Also can confirm that this is not only related to wsl2 - tried starting notepad.exe and had the same issue
The only thing I found that worked is to change the user on the task to the group "Users". The downside is that it opens a terminal on the screen. I can't seem to minimize it on execution for some reason. Strange how everyone has a different method that works.
@GlenC289 solution worked for me on Windows 11. I am copying my Task Scheduler Script. Comments indicate how I setup the Task. -NoExit argument to pwsh was necessary. I have installed wsl2 Ubuntu and enabled SystemD.
Code from the Filename: start-wsl-forward-ports.ps1 follows.
# WSL Start and Port Forwarding Script
# Task Scheduler Setup:
# Run as user: Users
# Run with highest privileges
# Triggers: Startup delay 30 seconds
# Action: script: "C:\Program Files\PowerShell\7\pwsh.exe"
# Arguments: -NoExit -WindowStyle hidden -ExecutionPolicy Bypass -File "C:\scripts\wsl2\start-wsl-forward-ports.ps1"
# Start in: C:\scripts\wsl2\
# Settings: (2 only) 1) Allow task to be run on demand, 2) if the running task does not end......
#
Get-Date | Out-File -FilePath "C:\scripts\wsl2\output.log" -Append
Echo "Start WSL and port forwarding..." | Out-File -FilePath "C:\scripts\wsl2\output.log" -Append
$wsl_ip = (C:\Windows\system32\wsl.exe -d "Ubuntu-22.04" --shell-type login hostname -I).split(" ")[0]
# Following line runs an infinite long running job, so that WSL does not time out and exit.
C:\Windows\system32\wsl.exe -d "Ubuntu-22.04" tail -n 1 -f /dev/null &
Echo "WSL Machine IP: ""$wsl_ip""" | Out-File -FilePath "C:\scripts\wsl2\output.log" -Append
# Put Your Ports Forwarding below and uncomment
#netsh interface portproxy add v4tov4 listenport=4505 connectport=32377 connectaddress=$wsl_ip
#netsh interface portproxy add v4tov4 listenport=4506 connectport=32378 connectaddress=$wsl_ip
#netsh interface portproxy add v4tov4 listenport=8092 connectport=32380 connectaddress=$wsl_ip
#Write-Host "Completed - Start WSL and port forwarding..."
Echo "Completed - Start WSL and port forwarding..." | Out-File -FilePath "C:\scripts\wsl2\output.log" -Append
@hammadrauf Thanks, I appreciate you taking the time to post your solution. I finally decided to remove the Microsoft Store version (as reported by cheng-chi) above and it has been working normally for me so far. I will keep your post in mind though, just in case MS carries the problem over to the windows version.