0%

0/1 Lessons

Course Introduction

• 5min

0 / 2 lessons complete

Getting Started with Windows PowerShell

• 56min

0 / 8 lessons complete

Getting Help and Finding Commands

• 39min

0 / 6 lessons complete

PowerShell Command Syntax

• 33min

0 / 5 lessons complete

PowerShell Objects and Properties

• 35min

0 / 6 lessons complete

The PowerShell Pipeline

• 24min

0 / 2 lessons complete

PowerShell Providers

• 30min

0 / 5 lessons complete

PowerShell Arrays and Variables

• 28min

0 / 4 lessons complete

PowerShell Loops

• 19min

0 / 3 lessons complete

PowerShell Conditional Statements

• 11min

0 / 1 lessons complete

On Premises Lab Setup

• 36min

0 / 8 lessons complete

Basic Domain Administration with Windows PowerShell

• 2hr 27min

0 / 10 lessons complete

Send Emails with PowerShell

• 22min

0 / 2 lessons complete

PowerShell Desired State Configuration (DSC) Basics

• 1hr 48min

0 / 6 lessons complete

PowerShell Modules

• 58min

0 / 7 lessons complete

Powershell Challenges

• 1hr 55min

0 / 23 lessons complete

Course Conclusion

• 1min

0 / 1 lessons complete

Instructions

Q&A (0)

Notes (0)

Resources (0)

Saving Progress...

Resources

There are no resources for this lesson.

Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.

Create note

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.

Now, we are going to select Run whether the user is logged in or not.

Other options are not necessary.

Now, we are going to click the Triggers tab and click on New.

This will tell the task scheduler when to kick off the script.

We are going to select One time and down below we select Repeat task every 1 minute for a duration of 15 minutes and click OK.

Now, we are going to the Actions tab and click New.

The Program we are going to call is not the script we wrote, it is actually PowerShell.exe. Now the Add arguments is going to be the exact path of the script C:\Scripts\AddContent.ps1

Click OK.

Now, there are no Conditions or Settings we need to change so I am going to click OK.

Now I need to type in the password for the Administrator account.

Now, what I am going to do is right-click the AddContent task and choose Run.

After executing the task we should see that under the Last Run Result the status is The operation completed successfully.

If we open File Explorer we can see under the Scripts folder a file named Content.txt is created just a minute ago.

If we open the Content.txt file we can see the time it ran when I run the script in the task scheduler.

What I am going to do is wait for a minute and see if it adds the additional content. And, after a minute we can see there’s a new line added.

So, for the next 15 minutes, this scheduled task is going to be running this script.

Now, you can see how this could be very useful.

We can click on the History tab in Task Scheduler and we can get the specific history of this task.

This is very useful when you are trying to troubleshoot.

And you can see why this is useful. If you need to run Windows Back Ups and you need them to run at a specific time and you write a script that has all this logic it is really nice if you create a scheduled task and have it run every hour or so. There’s no end to the things you can do when you are running scheduled tasks combined with the power of Windows PowerShell.

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
0 Comments
Inline Feedbacks
View all comments