Use Scheduled Tasks to Run PowerShell Commands on Windows

ScriptingGuy1

  

Summary: Learn how to use the Windows Task Scheduler to run Windows PowerShell commands automatically.

 

Hey, Scripting Guy! QuestionHey, Scripting Guy! One of the things that is a bit frustrating about reading the Hey, Scripting Guy! Blog is that it seems as if you expect network administrators to sit in front of their computer screens manually launching scripts and reading the input on their monitors. I do not know how long it has been since you were a network administrator, but that is not the way things are done anymore. Dude, I am lucky if get a chance to sit in front of my computer and read my email. In fact, most of the time I do not. I read my email on my Windows 7 smart phone as I hurry from one problem to the next, or as I shuttle from one meeting to the next. I could actually survive quite well without a desk, and would certainly be better off if I had no office for people to stick notes to the door. I do all my maintenance and monitoring via scheduled tasks. My question is how can I run Windows PowerShell via a scheduled task?

— JH

 

Hey, Scripting Guy! AnswerHello JH, Microsoft Scripting Guy Ed Wilson here. Well things have been really crazy in Charlotte, North Carolina this week. Whenever it snows (or even threatens to snow) in the Deep South, people seem to go crazy. The weather predicted snow for the weekend, and all the grocery stores were suddenly thronged. I, rather than haunting grocery stores, spent the weekend in my wood shop listening to classic music on my boom box, and meticulously hand planing burled walnut boards. Dr. Scripto headed out into the snow on Monday when the snow finally hit, and attempted to make a snow person. Unfortunately, the snow would not stick together, and therefore his efforts were thwarted. He is seen in the following figure.

 

JH, when the city is shutdown, I like to check the email sent to Scripter@Microsoft.Com. As it turns out, when I was a network administrator things were just as hectic as they are now. I also used scheduled tasks extensively to manage my servers. The reason I do not always talk about this, is that our readers come from a wide variety of backgrounds. Many are looking for scripts they can run immediately, and others are looking for sample code they can adapt to their specific needs.

JH, when you talk about using scheduled tasks to work with Windows PowerShell, there are at least two scenarios. The first is using a scheduled task to run a Windows PowerShell command, and the second scenario is using a scheduled task to run a Windows PowerShell script.

Today I am going to examine using a Scheduled task to run a Windows PowerShell command. It is the syntax of the command that can be a pain to work with. Therefore, I use the Start / Run command to test out my command prior to going to all the trouble to schedule it. I call PowerShell, specify the command parameter, and then use the ampersand, a pair of curly brackets, and the Windows PowerShell command I wish to run. This syntax is seen here.

powershell -command &{get-process}

 

When I open Start / Run, the dialog seen in the following figure appears.

 

The above command will open Windows PowerShell, run the Get-Process cmdlet, and close Windows PowerShell. Unless one reads really fast, this command is pretty useless. If I want the Windows PowerShell console to remain open, so that I can read what transpired, I need to use the noexit parameter. The thing that is really strange about the parameters for PowerShell.exe is that they must be received in a specific order. Therefore, noexit must come prior to the *command *parameter. This is seen here.

powershell -noexit -command &{get-process}

 

Of course, it does not make much sense to run a scheduled task, and then leave a Windows PowerShell console window lingering around afterwards. I like to redirect any output to a text file (or to a registry key, or to an event log, or database as appropriate). The easiest way to redirect to a text file, is to use the redirection arrows. Keep in mind that the redirection arrows can point to a text file that does not exist as the file will be created. However, the redirection arrows will not create a folder that does not exist. In addition, keep in mind that the redirection arrows go inside the curly brackets. This is seen here.

powershell -noexit -command &{get-process >>C:\fso\mytestprocesses.txt}

 

One redirection arrow will overwrite an existing file. Two redirection arrows will append to a previously existing file.

One of the cool things is that a semi-colon can separate a series of Windows PowerShell commands. The semi-colon marks an end of a line, and therefore an entire Windows PowerShell script could actually be incorporated into a single Windows PowerShell command. The advantage of this is the command is completely portable and not dependent upon access to a Windows PowerShell file. The following Windows PowerShell command will retrieve a listing of all running processes, all running services, as well as the bios version of the local computer. All this information will be written to a text file. Here is the command (this is one real long command that is copied from the run dialog. I have not attempted to “break” the code to appropriate lengths because cut and paste would not work into the Windows PowerShell console. As there are no returns in the below code, it will work with a paste into the run dialog, assuming you have a c:\fso directory).

powershell -noexit -command &{get-process >> c:\fso\ServiceProcessBios.txt; get-service | where{$_.Status -eq ‘Running’} >> c:\fso\ServiceProcessBios.txt; Get-WmiObject Win32_bios >> c:\fso\ServiceProcessBios.txt}

 

To create a scheduled task (I am using Windows 7) I open the Task Scheduler (from All Programs / Administrative Tools) and I create a basic task. I cheated a little bit, and pasted the above command line directly into the Program / Script to run box. A message will appear that asks if I intend to use the –noexit as an argument to the PowerShell executable. I say yes. The Start a program dialog appears (as seen in the following figure).

 

As seen in the following figure, the task scheduler divided the command up properly. If you wish to avoid the message about arguments, then you would use PowerShell as the program to start, and the remaining command as your argument. In addition, once the command is worked out properly, you will want to remove the –noexit portion of the command.

 

When scheduling the command, make sure you allow enough time in the future for you to be able to complete the wizard, and for the scheduled task engine to pick up the job. To see if the job completed successfully, look for the presence of the text file in the folder, or simply check the Scheduled Job History tab (seen in the following figure).

 

JH, that is all there is to using the task scheduler to run a Windows PowerShell command. Input week will continue tomorrow when I will talk about running scheduled tasks to run a Windows PowerShell script. I have a number of other posts about using Windows PowerShell to interact with Scheduled Tasks. You might find some of them interesting.

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

 

Ed Wilson, Microsoft Scripting Guy

0 comments

Discussion is closed.

Feedback usabilla icon