Use Scheduled Tasks to Run PowerShell Commands on Windows


Summary: Learn how to use the Windows Task Scheduler to run Windows PowerShell commands automatically.


Hey, Scripting Guy! QuestionHey, Scripting Guy! One of the things that is a bit frustrating about reading the Hey, Scripting Guy! Blog is that it seems as if you expect network administrators to sit in front of their computer screens manually launching scripts and reading the input on their monitors. I do not know how long it has been since you were a network administrator, but that is not the way things are done anymore. Dude, I am lucky if get a chance to sit in front of my computer and read my email. In fact, most of the time I do not. I read my email on my Windows 7 smart phone as I hurry from one problem to the next, or as I shuttle from one meeting to the next. I could actually survive quite well without a desk, and would certainly be better off if I had no office for people to stick notes to the door. I do all my maintenance and monitoring via scheduled tasks. My question is how can I run Windows PowerShell via a scheduled task?

— JH


Hey, Scripting Guy! AnswerHello JH, Microsoft Scripting Guy Ed Wilson here. Well things have been really crazy in Charlotte, North Carolina this week. Whenever it snows (or even threatens to snow) in the Deep South, people seem to go crazy. The weather predicted snow for the weekend, and all the grocery stores were suddenly thronged. I, rather than haunting grocery stores, spent the weekend in my wood shop listening to classic music on my boom box, and meticulously hand planing burled walnut boards. Dr. Scripto headed out into the snow on Monday when the snow finally hit, and attempted to make a snow person. Unfortunately, the snow would not stick together, and therefore his efforts were thwarted. He is seen in the following figure.


JH, when the city is shutdown, I like to check the email sent to Scripter@Microsoft.Com. As it turns out, when I was a network administrator things were just as hectic as they are now. I also used scheduled tasks extensively to manage my servers. The reason I do not always talk about this, is that our readers come from a wide variety of backgrounds. Many are looking for scripts they can run immediately, and others are looking for sample code they can adapt to their specific needs.

JH, when you talk about using scheduled tasks to work with Windows PowerShell, there are at least two scenarios. The first is using a scheduled task to run a Windows PowerShell command, and the second scenario is using a scheduled task to run a Windows PowerShell script.

Today I am going to examine using a Scheduled task to run a Windows PowerShell command. It is the syntax of the command that can be a pain to work with. Therefore, I use the Start / Run command to test out my command prior to going to all the trouble to schedule it. I call PowerShell, specify the command parameter, and then use the ampersand, a pair of curly brackets, and the Windows PowerShell command I wish to run. This syntax is seen here.

powershell -command &{get-process}


When I open Start / Run, the dialog seen in the following figure appears.


The above command will open Windows PowerShell, run the Get-Process cmdlet, and close Windows PowerShell. Unless one reads really fast, this command is pretty useless. If I want the Windows PowerShell console to remain open, so that I can read what transpired, I need to use the noexit parameter. The thing that is really strange about the parameters for PowerShell.exe is that they must be received in a specific order. Therefore, noexit must come prior to the command parameter. This is seen here.

powershell -noexit -command &{get-process}


Of course, it does not make much sense to run a scheduled task, and then leave a Windows PowerShell console window lingering around afterwards. I like to redirect any output to a text file (or to a registry key, or to an event log, or database as appropriate). The easiest way to redirect to a text file, is to use the redirection arrows. Keep in mind that the redirection arrows can point to a text file that does not exist as the file will be created. However, the redirection arrows will not create a folder that does not exist. In addition, keep in mind that the redirection arrows go inside the curly brackets. This is seen here.

powershell -noexit -command &{get-process >>C:\fso\mytestprocesses.txt}


One redirection arrow will overwrite an existing file. Two redirection arrows will append to a previously existing file.

One of the cool things is that a semi-colon can separate a series of Windows PowerShell commands. The semi-colon marks an end of a line, and therefore an entire Windows PowerShell script could actually be incorporated into a single Windows PowerShell command. The advantage of this is the command is completely portable and not dependent upon access to a Windows PowerShell file. The following Windows PowerShell command will retrieve a listing of all running processes, all running services, as well as the bios version of the local computer. All this information will be written to a text file. Here is the command (this is one real long command that is copied from the run dialog. I have not attempted to “break” the code to appropriate lengths because cut and paste would not work into the Windows PowerShell console. As there are no returns in the below code, it will work with a paste into the run dialog, assuming you have a c:\fso directory).

powershell -noexit -command &{get-process >> c:\fso\ServiceProcessBios.txt; get-service | where{$_.Status -eq ‘Running’} >> c:\fso\ServiceProcessBios.txt; Get-WmiObject Win32_bios >> c:\fso\ServiceProcessBios.txt}


To create a scheduled task (I am using Windows 7) I open the Task Scheduler (from All Programs / Administrative Tools) and I create a basic task. I cheated a little bit, and pasted the above command line directly into the Program / Script to run box. A message will appear that asks if I intend to use the –noexit as an argument to the PowerShell executable. I say yes. The Start a program dialog appears (as seen in the following figure).


As seen in the following figure, the task scheduler divided the command up properly. If you wish to avoid the message about arguments, then you would use PowerShell as the program to start, and the remaining command as your argument. In addition, once the command is worked out properly, you will want to remove the –noexit portion of the command.


When scheduling the command, make sure you allow enough time in the future for you to be able to complete the wizard, and for the scheduled task engine to pick up the job. To see if the job completed successfully, look for the presence of the text file in the folder, or simply check the Scheduled Job History tab (seen in the following figure).


JH, that is all there is to using the task scheduler to run a Windows PowerShell command. Input week will continue tomorrow when I will talk about running scheduled tasks to run a Windows PowerShell script. I have a number of other posts about using Windows PowerShell to interact with Scheduled Tasks. You might find some of them interesting.

I invite you to follow me on Twitter or Facebook. If you have any questions, send email to me at or post them on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.


Ed Wilson, Microsoft Scripting Guy

Comments (13)

  1. JerryLC says:

    @VasekB – Thanks, I'll take a look at the code.

    @VasekB@Nomadtales – Understood (ahead of time) the CMD shell option (I should have noted that). I am still very curious about whether the 'Start > Run' interface' string length can be extended. Also, I am VERY curious what the supported string lengths are for the Scheduled Tasks interface boxes.

  2. @JerryCruz no way I know of to increase max length. Can wrap PowerShell into a batch file and get around this…

  3. JerryLC says:

    I was testing your commands (Windows 7 Enterprise) and adding a few things like Set-Location and Write-Output when the commands suddenly stopped working. Apparently, the Start-Run interface supports a maximum string length of only 259 characters. So testing a command to run with that will be limited (even using the short version or alias of existing commands). Is there a way (perhaps via registry) to increase that maximum supported string length? Do you know if the Scheduled Task Program and/or arguments sections also have a maximum string length limitation?

  4. VasekB says:…/vr-cen-hodnot-z-funkce-v-powershellu-zp-t-do-dos-cmd-scriptu.aspx

    there is my post with solution how to run PowerShell scripts from DOS and how to retrun data without wrapping them by "DOS windows size". It's in Czech language only, sorry. But example code is enough for understanding, I think ;o)

  5. Nomadtales says:

    @JerryCruz You could of course run the command from a command prompt.

  6. VasekB says:

    @Jerry Cruz – Command prompt (Cmd. exe) command-line string limitation (…/en-us )

  7. Ish Sierra says:

    Curious if anyone came across this behavior when running in task scheduler or in run box on Server 2008.  I created a script called custom.ps1 to compress a folder so I run the following to feed it the list of items. "powershell -command &{gi e:pathname -include filename* | c:scriptscustom.ps1 $_}".  It starts to run calls my script and I see the zip file, but there is nothing in it.  The powershell window disappears quickly.  If I add the "-noexit" switch it runs normally.  This also happens in Task Scheduler.  Anyone have ideas what might be happening?

  8. Dean says:

    I was wondering if there was a way to run a scheduled task, that would run a powershell script saved in a ps1 file, but that would actually open the console also so I could keep checking on the progress?

  9. Mervin says:

    Hi, I tried this on my pc but is running slow….but when I ran this manually it is running fine….how do I solved this?

  10. srikanth says:

    But in my windows 2008 machine it is not working, but there is no error. and it is working when I tested in "RUN", let me is there any other argument.

  11. Matt Borja says:

    Just FYI, you may want to do a test run from Scheduled Tasks and watch the status, especially if you’re doing one-shot Powershell commands like this:

    Name: Invoke WordPress Cron
    Action: powershell -ExecutionPolicy Bypass -Command & { [System.Net.WebRequest]::Create(‘‘).GetResponse() }

    If after running it, you know the task should have stopped by a certain amount of time but it hasn’t, it’s likely because Task Scheduler hasn’t received an expected exit code. This condition will prevent the task from running again (if it’s supposed to be configured
    as recurring) but can be easily fixed as shown in the revised action below:

    Action: powershell -ExecutionPolicy Bypass -Command & { [System.Net.WebRequest]::Create(‘‘).GetResponse(); Exit 0 }

  12. Steve says:

    It wouldn’t work for me unless I had a space after the ampersand, and quoted it all. From the help text:

    To write a string that runs a Windows PowerShell command, use the format:
    where the quotation marks indicate a string and the invoke operator (&)
    causes the command to be executed.

    Possibly a difference in versions since this is from 2011.

    1. Steve says:

      It stripped out some text from my comment but run

      powershell -?

      …for a working example.