Skip navigation

How to Schedule PowerShell Scripts

One of the top questions I get from students is, "how do I schedule a PowerShell script?" It's actually easier than you might think.

Start by opening Cmd.exe - and believe me, it isn't often that I start an article with that instruction. Run powershell /? to see the command-line parameters for the PowerShell.exe console host. You'll notice a couple of key parameters, including one to let you specify a command that should be run (-command) and one that specifies the path and filename of a script file that should be run. When you use these parameters, the shell will open, run your command and/or run your script, and then close. So you just schedule PowerShell.exe with the appropriate command-line parameters, and you're good to go.

PowerShell's normal script execution policy applies, so be sure to enable scripting (either RemoteSigned or AllSigned) first. Alternately, if you want scripts to be normally disabled on the machine, you can specify an alternate execution policy via command-line parameter, the same way you'd specify a command or script file. The execution policy you specify will only be active for that one PowerShell session, enabling it to run your scheduled script. 

And that's all there is to it. The .PS1 filename extension isn't considered executable by Windows, so you don't schedule scripts directly - you schedule PowerShell, and tell it via command-line parameter which script to run. Pretty much the same way Windows Script Host (CScript.exe or WScript.exe) worked, in fact. 



Want to learn more tricks like that? My PowerShell retreat is full of them, along with all the core information you need to make PowerShell work harder for you. Check it out now, as space is limited!
Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish