How To Schedule Tasks for PowerShell Scripts
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
- Step 1a: Test Your Code in PowerShell
- Step 2: Save Your Payload File
- Step 3: Launch the Task Scheduler
- Step 3a: Configure the Actions Tab
- Step 4: Testing Your Task
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.
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.
Save the above PowerShell script as say – ProcessTask.ps1. Take a note of your full path: e.g. D:\PShell\ProcessTask.ps1
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.
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:
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)
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.
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
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!
I hope these help you, in particular, you may need the -ExecutionPolicy bypass parameter.
Sets the default execution policy for the current session.
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.
Hides the copyright banner at startup.
Does not present an interactive prompt to the user.
Does not load the Windows PowerShell profile.
Starts the specified version of Windows PowerShell. Enter a version number with the parameter, such as "-version 1.0".
Sets the window style to Normal, Minimized, Maximized or Hidden.
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.
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:
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.