Running Powershell Scripts as Scheduled Tasks
Server Academy Members Only
Sorry, this lesson is only available to Server Academy Full Access members. Upgrade your plan to get instant access to this and many more premium courses. Click the Upgrade Plan button below to get started.

Saving Progress...
In this lecture, I am going to show you how to create scheduled tasks to run your PowerShell scripts at a specific time on a general basis or just one time whatever the case might be.
So, what we are going to do is we are going to create a simple script that is going to add data to a text file and we are going to call the script to run every minute so that it gives us a good example how to create the scheduled tasks, how to write the scripts, and how to get them to execute.
So, the first thing we are going to do is open the PowerShell editor by hitting Start in the bottom left corner.
Now, I am going to type in powershell, and we are going to run the PowerShell Intelligent Scripting Environment or PowerShell ISE.

Once this loads we can begin writing the script.

We are going to do a simple single-line command that is going to add some text followed by the date. Let’s start with the following and press Play.
$date = date
$content = “This is some cool content $date”

In the bottom pane if we type:
echo $content
This is the result we are going to get each time.

Now, what we need to do is continue editing adding:
$date = date
$content = “This is some cool content $date”
Add-Content C:\Scripts\Content.txt “$content”
exit
Click File > Save to save the script.

And, we are going to This PC > C:\ > and create a new folder called Scripts and name it AddContent.ps1. Click on Save.

Now, if we navigate to the C:\Scripts folder and run the script we should have a new text file called Content.txt

If we open this text file we can see the following:

If we execute the PowerShell script it should add another line. And, we can see the time difference when it was executed.

What I am going to do now is delete the Content.txt and close PowerShell ISE.
Now that we have the script created we need to create a scheduled task to call the script to run every minute or so.
I am going to do that by going to Server Manager > Tools > Task Scheduler

Left-click on the Task Scheduler Library and select Create Task…

In the Name type AddContent trying to keep the naming consistent with what we are doing. Next, in the Description add This adds some content to a txt file.

The next thing we want to do is Change the User or Group, or the user that is running the script. In the search box type Administrator and click on Check Name and select the user account Administrator. Generally, you would want to create a special user account for when you are making scheduled tasks, you generally don’t want to use the Administrator account, and more than that you don’t want to use your own user account.
If I were to set a scheduled task for something like Windows Backup and my user account got locked out because I typed in the wrong password too many times then all the scheduled tasks are going to fail because that user account is locked out.
So, it's generally important that you create a new user account that will have the least amount of privileges necessary for it to complete the running of the script or the operation of the script.
Now, I am using the Administrator account because I don’t want to go through all the work and the hassle of creating a new user account for this simple demonstration, but just keep that in mind for the future.
Server Academy Members Only
Want to access this lesson? Just sign up for a free Server Academy account and you'll be on your way. Already have an account? Click the Sign Up Free button to get started..