Weekend Scripter: Use the Windows Task Scheduler to Run a Windows PowerShell Script

Summary: Create a scheduled job to run a Windows PowerShell script that creates an HTML Server uptime report

Microsoft Scripting Guy, Ed Wilson, is here. The amount of emails that are generated when attempting to organize and to arrange an event such as PowerShell Saturday is amazing. Every day, it seems, there are nearly a dozen email messages about one detail or another. Windows PowerShell Microsoft MVP, Jim Christopher, is doing an excellent job of keeping things on track, and the Scripting Wife and Brian Wilhite have been hanging right in there with the work. The event truly will be a major big deal, and it will be fun as well as educational. It will be worth the time to register and to attend this event if you are in the Charlotte, North Carolina area on September 15, 2012.

Creating a Scheduled task to run a PowerShell script

The first thing I need to create a scheduled task to run a Windows PowerShell script is the command line that I will execute. The easy way to find this is to use the Run command. At times, I need to know what the command-line switches are for PowerShell.exe. To see these, I open Windows PowerShell and type powershell /? and then I examine the output that displays. The command and the output from the command are shown here.

Image of command output

When I know which switches to use, I practice my command via Run. The following image illustrates using Run to launch Windows PowerShell and to run a Windows PowerShell script. Keep in mind that this will open, and close Windows PowerShell, which is fine for a script producing a report. In testing, I often use the –noexit switch to see any errors arising from the operation.

Image of message

When I know the command line, I use the Task Scheduler tool, and create a new basic task. First, I need to assign a name and a description. I find it useful to provide a good description as well as a decent name because it facilitates performing maintenance on the task.

Image of menu

The next pane is the Task Trigger pane. It is pretty basic, and self-explanatory. Because I want to create a daily task, I leave that selected. After it is created, it is easy to edit the scheduled task to make it run the task more often, such as every hour if that is the need. One reason I use the Basic Task Wizard is that it is easy to get through the steps needed to create the basic task. I always edit stuff later. The Task Trigger pane is shown here.

Image of menu

Now it is time to set the schedule for the task. In this example, the task runs every morning at 7:00 AM beginning on August 11, 2012.

Image of menu

In the Action pane that follows, I select that we want the scheduled task to Start a program, and then click Next.

Image of menu

In the Start a Program pane, I cheat by placing the command I tested previously from the Run box into the Program/script box. I then click Next. The Start a Program pane is shown here.

Image of menu

Here is where the cheating part comes in to play. I used, directly, the command I tested in the Run box for my program. Rather than attempting to break things up, I simply copied the entire line. The Scheduled Task Wizard is smart enough to know what I wanted to do. It prompts, but it knows. The prompt appears here.

Image of message

When I have completed the Create Basic Task Wizard, I want to open the task and make a couple of additional changes. The easy way to do this is to select the Open the Properties dialog for this task when I click Finish, as shown here.

Image of menu

Because the task runs on a server, and because one might not be logged on to the server at the time the task is to run, it makes sense to tell the task to run whether or not the user is logged on. This opens a credential dialog, and allows me to set the password for the task. This option appears on the General tab of the scheduled job as shown in the image here.

Image of menu

When I have completed configuring the scheduled task, I always right-click the job and select Run. Then I examine the job history to ensure that the task completed properly. The History tab of the scheduled job is shown here.

Image of menu

Well, that is about all there is to creating a scheduled job to run a Windows PowerShell script. In Windows 8 and Windows Server 2012, there are Windows PowerShell cmdlets to create the scheduled job and to create the job triggers and actions—but that will be the subject of a later Hey, Scripting Guy! Blog post.

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

Ed Wilson, Microsoft Scripting Guy 

Comments (31)

  1. Rafa David says:

    Hi Ed,

    Maybe it's useful for someone:

    I was trying to test it and it didn't work for me. I had to pass the name of the file as an argument and without the switch "-file".

    I searched it on the Internet and it seems like more people have had the same problem…

    1. Darek says:

      One thing to note… When you pass the name of the file that stores the PoSh code to exec to powershell.exe, please use DOUBLE QUOTES. If you use single quotes, Task Scheduler will not exec this properly. So, it should be something like: > powershell.exe -file “C:\My Folder\My File.ps1”. Also, you want to exec this under credentials of an account that has the Run as Batch Job. Somebody has already mentioned this here…

  2. vdinenna says:

    Thanks!  Of all the explainations, this one worked for me and it was the simplest as well.

  3. K_Schulte says:

    Hi Ed,

    it's a great idea that we can use the task scheduler to integrate our powershell jobs!

    An easy way to get things done without pressing the button 🙂

    It might be even more comfortable if we can build schedule directly via PS cmdlets …


  4. mredwilson says:

    @K_Schulte in Windows PowerShell 3 there are job scheduler cmdlets and it will allow you to create a scheduled task via cmdlets. They work great.

  5. Hi Ed, thanks for another great article.

  6. kiquenet says:

    I need create schedule task programatically using Powershell (PS remoting).

    My task execute a powershell script (and other cases a script cmd) inmediately.

    $powershellcmd = (get-command powershell.exe).Definition

    $upgradeWSPps1 = (Join-Path $ScriptDirectory Tests.LifeCycle.Deploy.MOSS.UpgradeWSP.ps1)

    $cmdToRun = "$powershellcmd -ExecutionPolicy Bypass -WindowStyle Hidden -NoLogo -File ""$upgradeWSPps1"" "

    Maybe problems with single-quotes, double-quotes..

    Any suggestions about it for create task ?

  7. laderio says:

    I have got a powershell script which runs just finde when "Run only when user is loggen on" is selected. But when selecting "Run whether user is loggen on or not" the script does not run properly anymore.

    The script makes some AD queries and gets me the clusters in our AD. Then it queries the clusters for nodes and some status infos and also queries the nodes for events from failover clustering. Then it puts all in excel and saves the excel to local disk. Then it sends the excel file via smtp with the health of the clusters.

    The email is sent, when "Run whether user is loggen on or not" is selected, but no excel file is generated.

    Do you have any hints on that? If helpful, I could post the code of the script.

    1. The account you are using to run a scheduled job must have the “run as a batch job” right if you are running the job when the account is not logged on.

  8. John Brines says:

    Thanks guy that helped me out a bunch.


  9. Abrar Khan says:

    Excellent Work Done. I was wondering from so many days but it really helped me out

  10. Eva B says:

    Hi there, I am at a loss as to why, when running my powershell script with a scheduled task, the output gets truncated. My script outputs text to a file and one of the lines in my text (which is in xml format) gets truncated.

    When I run my script without using a scheduled task, it works fine.

    Here is the output of my file (just an excerpt because it is long):

    As you can see, the " totalNo…" is what is getting truncated. It should look like this:


    Any ideas? I have spent 3 weeks perfecting my script to automate a huge process at work and now, the only thing that is stopping me is this frustrating issue!

    Thanks, any help is appreciated.

  11. Eva B says:

    Sorry, this editor stripped out my XML from my message. Basically, one of my XML tags is ending with "…" rather than with a proper closing tag.

  12. Script for scheduled task triggering on weekly says:

    We are implemented software asset management to a client, for software license data collection using ndtrack.ini scripts. Instead of creating scheduled task through “Create Basic Task Wizard” on each managed device, is it possible to script the task schedule
    triggering process on weekly basis? Do anyone have script for that?

  13. MBOULSTR says:

    Wow! Thanks for this! I’ve been trying to schedule my script to run and it just wouldn’t. Followed these instructions and boom it works!

  14. Michael H says:

    If you use a domain account be sure that it has "Log on As a batch job" user rights assignment.

  15. Grunge says:

    Greetings to Microsoft:
    Would be great if the Task Scheduler could handle PowerShell files directly and there returns.
    This would be easier for Arguments and returning a fail status.

  16. Ben says:

    Thank you, this was very helpful

  17. jessie says:

    Thanks for the post, it really saved my day

  18. Bhuvan says:

    Thank you. Fixed my issues with running the PS script in Task Scheduler.

  19. Dave Robinson says:

    I tried Ed’s instruction exactly step by step, and am STILL unable to run my powershell command from the Task Scheduler in Windows. Bur when I manually execute the script, it works as it should. I have tried at least 15 different solution suggested on
    the Internet, but still I cannot run this damned script via the Task Scheduler. So frustrating!!!

  20. Anonymous says:

      Context A common problematic encountered in complex development projects is to handle CRM customizations

  21. Finn Holm says:

    You’re a champ!works like a charm.Thanks

  22. Gaetan says:

    Quick query. I’m simulating a failure in the script to check task completion. How exactly would I reflect the failure back in the task history ?

    The script is simple "send-mailmessage -from {sender} -to {recipient} -subject ‘test’ -body ‘test’ -deliverynotificationoption onfailure". The task is running well. How do I see the task failing if send-mailmessage fail somehow ??

  23. niepelnosprawny.org says:

    wil it be working on ssh?

  24. Clint says:

    Hi Ed, great article and just what I was looking for. However……the task completes with success but my file isn’t moved or renamed as my powershell script should do. If I run the exact script in a powershell from the prompt it completes fine with no
    issues. I am using a domain account and have it set as such. Any ideas?

  25. BadLeo says:

    This and only this worked for me on Windows server 2012

    powershell.exe -ExecutionPolicy Bypass -Command “”

  26. DaveNGA says:

    You are a life saver! This trick worked perfectly on my 2k12 Hyper-V host.

  27. vishal says:

    Nice Script !! Thanks for your efforts

Skip to main content