Guy recommends :
Free SolarWinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



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, run your PowerShell script with the built-in Windows Task Scheduler.

 ♣

Step 1: Create the PowerShell Payload File

This is just an example PowerShell script for the Task Scheduler. While 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 by running the command in the PowerShell ISE.  In particular I would like to see that SchedulePr.txt was created and 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 for finding the built-in Windows Task Scheduler. Once launched seek the Actions pane then Create Task  (Swerve the basic task.)PowerShell Task Scheduler

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.

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'.

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.

Names of Recap:

  • 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: WMI Monitor and It's Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.  Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server.

Download your free copy of WMI 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

Guy Recommends:  SolarWinds' Free Bulk Import ToolFree Download of Solarwinds  Bulk Import Tool

Import users from a spreadsheet.  Just provide a list of the users with their fields in the top row, and save as .csv file.  Then launch this FREE utility and match your fields with AD's attributes, click to import the users.  Optionally, you can provide the name of the OU where the new accounts will be born.

There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:

  1. Bulk-import new users into Active Directory.
  2. Seek and zap unwanted user accounts.
  3. Find inactive computers.

Download your FREE bulk import tool.

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

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.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, SolarWinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2012 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: