PowerShell 3.0 Schedule Task

How To Schedule Tasks for PowerShell Scripts

The situation: You have a PowerShell 3.0 script that you want to run every day, or every week.  Alternatively, you want to execute PowerShell 3.0 commands after a particular event occurs.PowerShell Task Scheduler

My advice is don’t re-Invent the wheel; take advantage of the built-in Windows Task Scheduler.  I also believe in horses for courses, PowerShell is great for scripting, while Task Scheduler is for setting Actions and Triggers.

 ♣

Step 1: Create the PowerShell Payload File

This is just an example of a PowerShell script to feed to the Task Scheduler.  Although the script is feeble, and ripe for modification, at least it produces output in the form text file containing a list of processes.

# PowerShell Create Schedule Task
$Location = "C:\SchedulePr.txt"
Get-Process | Out-File $Location -append

Note 1: I used $Location to remind us to change the path to the file created by this script.

Step 1a: Test Your Code in PowerShell

Before you ask the Task Scheduler to run this code I would test the command by running the command in the PowerShell ISE.  In particular I would like to see that SchedulePr.txt was created, and I like to take note of its Date Modified.

Step 2: Save Your Payload File

Save the above PowerShell script as say – ProcessTask.ps1.  Take a note of your full path:  e.g. D:\PShell\ProcessTask.ps1

Step 3: Launch the Task Scheduler

I use the Windows ‘Search’ dialog box to locate the built-in Windows Task Scheduler. Once launched seek the Actions pane then Create Task  (Swerve the basic task.)PowerShell Task Scheduler for Windows 8

General: Give the task a name, e.g. GuyProc.  Whilst at this tab check the options.

Trigger: We will fire the PowerShell script at a specified time, e.g. 12:30 every day.

Actions: This is where we instruct the scheduler what to do when the trigger fires, namely run a PowerShell script.

Step 3a: Configure the Actions TabPowerShell Task Scheduler

We have reached the crucial stage, the key point is to type just ‘PowerShell’ in the Program/script dialog box. See screenshot below right.

Mistake: Don’t put the name of the script in the Program/script box, just leave one instruction here ‘PowerShell’.  Study screenshot opposite.

Add arguments: This is where you tell PowerShell what to through parameters:
-File D:\PShell\ProcessTask.ps1

or better enclosed with double quote speech marks

 -File "D:\PShell\Process Task.ps1"

The "Double Speech Marks" are required if the path has spaces.  Incidentally, ‘ Single speech marks’ don’t cut it here.

-ExecutionPolicy Bypass will ensure that the PowerShell scheduled task will run even thought the computer has a restricted script policy.  However, there is a secret, and that is to ensure this parameter is at the beginning of the arguments thus:
-ExecutionPolicy Bypass -File "D:\PShell\Process Task.ps1"

See more PowerShell options here.

Recap of the Names:

  • What’s the name of your PowerShell .ps1 file?  (D:\PShell\ProcessTask.ps1)
  • Where to look for the output file?  ($Location = "C:\SchedulePr.txt")
  • Name of the Task (GuyProc)

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Step 4: Testing Your PowerShell Task

PowerShell Run Task Scheduler

This tip saved me a lot of time in testing tasks: Right-click and ‘Run’.

Once this works, then by all means revert to creating a timetable under the Trigger tab so that you can schedule a PowerShell script.

If the task fails even though it works in the PowerShell ISE, then I would consider appending -ExcecutionPolicy Bypass

Other ideas for tasks to schedule

Engineer's Toolset v10Guy Recommends: SolarWinds Engineer’s Toolset v10

This Engineer’s Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems.  Guy says it helps me monitor what’s occurring on the network, and each tool teaches me more about how the underlying system operates.

There are so many good gadgets; it’s like having free rein of a sweetshop.  Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools.  Try the SolarWinds Engineer’s Toolset now!

Download your fully functional trial copy of the Engineer’s Toolset v10

More PowerShell Parameters

I hope these help you, in particular, you may need the -ExecutionPolicy bypass parameter.

-ExecutionPolicy
Sets the default execution policy for the current session.

-File
Runs the specified script in the local scope, so that the functions and variables that the script creates are available in the current session. Enter the script file path and any parameters. File must be the last parameter in the command, because all characters typed after the File parameter name are interpreted as the script file path followed by the script parameters.

-NoLogo
Hides the copyright banner at startup.

-NonInteractive
Does not present an interactive prompt to the user.

-NoProfile
Does not load the Windows PowerShell profile.

-Version
Starts the specified version of Windows PowerShell. Enter a version number with the parameter, such as "-version 1.0".

-WindowStyle
Sets the window style to Normal, Minimized, Maximized or Hidden.

-Command
Executes the specified commands (and any parameters) as though they were typed at the Windows PowerShell command prompt, and then exits, unless NoExit is specified. The value of Command can be "-", a string. or a script block.

If the value of Command is "-", the command text is read from standard input.

If the value of Command is a string, Command must be the last parameter in the command , because any characters typed after the command are interpreted as the command arguments.

See real-life tasks for PowerShell »

Summary: Creating a PowerShell Task Schedule

If you want to run a PowerShell 3.0 script run every day, or every week then don’t re-Invent the wheel, call for the assistance of the Windows Task Scheduler.  Remember that the name of the Script / Program is PowerShell, the actual script is referenced by the -file parameter.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell tutorials:

PowerShell Home   • Test-ServerHealth  • Test-SystemHealth   • Test-Connection  • Test-Path

PowerShell Logon Script  • PowerShell add printer  • PowerShell Schedule Task  • Free CSVDE Tool

Map Network Drive  • Exchange 2010 PowerShell Cmdlets   • Exchange 2010 Performance Monitor

Please email me if you have a better script examples. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.