Sharon Kariwo

How To Automate your PowerShell scripts with Windows Task Scheduler

How To Automate your PowerShell scripts with Windows Task Scheduler

Automating PowerShell Scripts with Windows Scheduled Tasks

In today's digital age, automation is not just a luxury; it's a necessity for improving efficiency and allowing IT professionals more time to focus on strategic tasks.

GET A MENTOR TO HELP YOU ON YOUR TECH CAREER PATH

Automating routine tasks using PowerShell scripts can greatly enhance productivity and ensure critical operations are performed on schedule without manual intervention. This article, inspired by a recent tutorial from Shane Young at Bold Zebras, will guide you through the steps of saving a PowerShell script to a file and setting it up to run automatically via a Windows Scheduled Task.

Step 1: Write and Save Your PowerShell Script

The first step to automation with PowerShell is writing your script. You can automate virtually any task that can be managed through PowerShell, from simple file management tasks to complex system monitoring. Here’s a basic script to get you started:

This script gets the current date and time and writes it to a log file along with the available disk space on all drives.
Get-Date | Out-File -FilePath E:\demo\LogForVideo.txt -Append
Get-PSDrive -PSProvider FileSystem | Out-File -FilePath E:\demo\LogForVideo.txt -Append

This script captures the current system date and the available disk space on all drives, appending this information to a log file. You need to save this script to a .ps1 file, which is the standard extension for PowerShell scripts. In this example, we could save it as VideoPS.ps1.

Step 2: Create a Scheduled Task

Once your script is ready and saved, the next step is to automate its execution using the Task Scheduler in Windows. Here's how to do it:

Open Task Scheduler

  • Type "Task Scheduler" in the Windows search bar and open the app.
  • Once inside, select “Create Basic Task” to start the wizard.

Configure the Task

  • Name and Description: Give your task a meaningful name and description to help identify its purpose.
  • Trigger: Choose when the task should run. This could be daily, weekly, monthly, or even in response to a specific event.
  • Action: Select “Start a program.” For the program/script field, input PowerShell.exe. This action tells Task Scheduler to run PowerShell.

Add Arguments

In the "Add arguments" field, input the path to your script. For example:

-File E:\demo\VideoPS.ps1

This argument tells PowerShell to execute your script file.

Finalize and Test

  • Review your settings and click "Finish" to save the task.
  • Right-click the task and select "Run" to test if it executes properly.

What This Setup Achieves

By setting up a scheduled task, your PowerShell script will run automatically at the specified times without any further input from you. This setup is perfect for tasks like log management, system checks, updates, or any routine task that needs to run on a schedule.

Advantages of Automation

  • Efficiency: Automates routine tasks and frees up time for more complex problems.
  • Consistency: Ensures tasks are performed consistently and without human error.
  • Availability: Keeps critical tasks running in the background, even out of office hours.

Conclusion

PowerShell combined with Windows Task Scheduler provides a powerful toolset for automating routine tasks on a Windows system. Whether you're managing a single machine or an entire network, these tools can help streamline your processes and make your system management more efficient.

Remember, every minute saved by automation is a minute that can be spent on innovation or improvement. So set up your scripts, schedule them, and let them do the work for you while you focus on bigger and better things.