Run powershell commands in windows schedule task

How To

 

Windows schedule task is one of the best option to schedule background jobs to execute on certain time. schedule task allow us to run any program / script under Action tab.

if you want to run powershell then you have two options:

1> Run powershell script with schedule task

2> Run powershell commands in schedule task

So lets take a look how we can run specific commands or powershell script (.ps1) in schedule task.

1> Run powershell script with schedule task : –

Running powershell script is pretty easy, you can create your powershell script and save it to local machine with .ps1 extension. now the next step is to add the script in schedule task Action. you can click on New action to add new powershell script in schedule task and add powershell.exe under program/script as shown in below screenshot. now you can pass the script file path in argument like “-file “C:\TEMP\new-post-mk.ps1” -WindowStyle hidden

Full syntex is – Powershell.exe -file “C:\TEMP\new-post-mk.ps1” -WindowStyle hidden

Powershelltalk.com

 

 

Finally the action will look like as below:

 

2> Run powershell commands in schedule task

Running specific commands are little different then running powershell script under schedule task. instead of -file you can use -command as argument to powershell.exe and pass the actual command in &{ pscmd } format. below is basic example of running get-process command in schedule task.

 

powershell -command &{get-process}

Let check how we can add multiple commands and pipeline in schedule task. below is example where you can add multiple commands in the task as well as you can perform pipeline operations.

powershell -noexit -command &{get-process >> c:\temp\allprocess.txt; get-service | where{$_.Status -eq ‘Running’}}

 

finally the powershell command will look like as below:

if you are looking for information on how to create schedule task to run powershell invoke-webrequest or curl commands then check – https://powershelltalk.com/2022/01/24/run-powershell-commands-in-windows-schedule-task/


(Visited 47 times, 1 visits today)