Windows Server How-To
How To Automatically Run PowerShell Scripts at a Scheduled Time
The trick is to getting your run prompt to run correctly in the Windows Task Scheduler.
Over the last few years, I have created a vast library of PowerShell scripts that I use to keep my servers healthy. These scripts do everything from monitoring disk health to helping me to reclaim resources from virtual machines that I no longer use. The thing is, however, that in order for these scripts to be of any real benefit, they have to run automatically. I have neither the time, nor the desire to spend an hour each week manually running PowerShell scripts and analyzing the results. Thankfully, I don't have to. Windows makes it possible to schedule PowerShell scripts to run at a predetermined time.
The mechanism of choice for automating PowerShell scripts is the Windows Task Scheduler. The Windows Task Scheduler has been a part of the Windows operating system for many years. It is a simple tool for scheduling applications to run at a predetermined time.
So the strategy is to use the Windows Task Scheduler to run a PowerShell script. That's the end of the story, right? Well, not quite. There is one tiny detail that needs to be addressed.
The Task Scheduler really isn't all that different from the Windows Run prompt. The only real difference is that when you enter a command into the Run prompt, the command executes right away. When you enter a command into the Task Scheduler, the command executes at the scheduled time. So with that said, have you ever tried to execute a PowerShell script from the Run prompt? It doesn't work in quite the way that you might expect.
Normally, you can execute a PowerShell script by typing ./ followed by the name of the script. However, this doesn't work from the Run prompt. The reason for this is that the Run prompt works more similarly to the old-school Windows Command Prompt than to PowerShell. To show you what I mean, I created a very simple PowerShell script at C:\Scripts called Hello.ps1. If I enter ./C:\Scripts\Hello.ps1 at the Windows Run prompt, I receive the error message shown in Figure 1.
OK, so what happens if you try entering the script's path and filename without specifying .\? If you try that, then the script opens in Notepad (at least that's the behavior on my machine). So how can we get the script to run?
The reason why neither of the previously mentioned methods work for running PowerShell scripts is because neither technique invokes PowerShell. The question therefore becomes a matter of how we can structure a command so that it launches PowerShell and then runs the script?
Well, the first part of the problem is easy to solve. You can invoke PowerShell simply by typing PowerShell at the Run prompt. Now, all we have to do is to launch a script along with PowerShell.
So here is the trick: Powershell.exe accepts command line arguments. Just try opening a Command Prompt window and typing PowerShell.exe /?. You can see some of the available arguments shown in Figure 2.
Take a look at the bottom of the previous screen capture. As you can see in the figure, the –File switch allows you to run a script as you launch PowerShell. Therefore, if I wanted to launch my Hello.ps1 script from the Run prompt, then I could do so by using this command:
PowerShell –File "C:\Scripts\Hello.ps1"
This same technique can be used with Windows Task Scheduler. Take a look at Figure 3. As you can see, the Create Basic Task Wizard contains a field in which you can enter the command that you want to run. There is also an optional field that you can use if you need to add an argument to your command.
So as you can see, PowerShell scripts can be automated through the Windows Task Scheduler. The trick to doing so is to perfect your command at the Run prompt before trying to schedule the task. If you run into problems related to PowerShell closing as soon as your script runs, you can use the –NoExit argument, but this argument must be specified before the –File argument.
Brien Posey is a 16-time Microsoft MVP with decades of IT experience. As a freelance writer, Posey has written thousands of articles and contributed to several dozen books on a wide variety of IT topics. Prior to going freelance, Posey was a CIO for a national chain of hospitals and health care facilities. He has also served as a network administrator for some of the country's largest insurance companies and for the Department of Defense at Fort Knox. In addition to his continued work in IT, Posey has spent the last several years actively training as a commercial scientist-astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space. You can follow his spaceflight training on his Web site.