Create a PowerShell scheduled task

Review the Windows Task Scheduler setup recorded for a PowerShell 7 task. The source warns that the Windows Store installation of pwsh.exe did not work for its scheduled tasks.

Author: Jason Treadwell

The source uses PowerShell 7 (pwsh.exe) for its scheduled tasks.

WARNING: If pwsh.exe has been installed via the MS Windows App Store, the pwsh.exe will not run.

Task Scheduler

Create Powershell folder for your tasks

Open "Task Scheduler" (taskschd.msc) Right Click on "Task Scheduler Library" Select "New Folder" Type in Powershell and press the Ok button

Screenshot retained for review

Create Basic Task

Right click the Powershell Folder and select Create Basic Task...

Screenshot retained for review

Set Task Name and Description

Task Name: NOTE After the Name is set you can't change it without exporting the task, importing the task, deleting the old task. The captured example is named "M365 Company Devices By School Best Effort"

Screenshot retained for review

Task Description: The source puts the function name, Get-CompanyDevicesBySchoolBestEffort, in the description so it is visible when selecting the task.

Example of what you see when you click on a task in Task Scheduler

Screenshot retained for review

Click NEXT

Set Trigger

Set trigger to Weekly

Click Next

Screenshot retained for review

Select Days and Start Time

Click Next

Screenshot retained for review

Select Start a program (should be selected by default) Click Next

Screenshot retained for review

Set Pwsh.exe as the program to run

Program:

pwsh.exe

Add arguments**:**

-WindowsStyle Hidden -command "Get-CompanyDevicesBySchoolBestEffort"

Click Next

Screenshot retained for review

Tick the box next to "Open the Properties dialog box for this task when I click Finish"

Screenshot retained for review

Click Finish

OPTINAL PROPERTIES - Run When Not Logged In

- Select the Radio button as circled below. "Run whether user is logged on or not"
  • Don't store the password Unless
  • You are trying to access a remote device during the script.
  • Avoid this.
  • Your internet connection requires you to be signed into to be able to access the Internet. You could use a non admin/IT account to get your internet access. If a script is run from an on-prem server then it's IP is typically allowed on to the Internet without user auth so you shouldn't need to store the password.
  • Optional but not ideal.
  • If the script authenticates to M365 through an enterprise App using a certificate, you don't need to store a password.
  • Ideally
  • Once you have a script that can run through an M365 Enterprise App you should run it in a Runbook or similar and have the script use creds that are stored in Azure Key Vault.
  • The source records incomplete adoption as at 13 March 2026; current adoption has not been checked.

Screenshot retained for review