Creating a Simple Automation Script Using PowerShell and Windows Task Scheduler


What is PowerShell?

Windows PowerShell is an object-oriented command environment that has scripting language features that use a program called cmdlets to simplify the administration, configuration, and management of various environments in both remote and standalone topologies by using standard-based remote protocols.

How to open PowerShell?

Click on Start and search for PowerShell.

PowerShell is supported by various platforms:

PlatformVersion- 7.0Version- 7.1
Windows 8.1 and 10YesYes
Windows Server 2012 R2, 2016, 2019YesYes
Ubuntu 16.04, 18.04YesYes
Ubuntu 20.04NoYes
Debian 9, 10YesYes
CentOS 7,8YesYes
Red Hat Enterprise Linux 7, 8YesYes
Fedora 31+YesNo
MacOS 10.13+YesYes

Role in Automation:

PowerShell is used as a scripting tool. The major benefit of using PowerShell is its ability to remotely connect to another system. Any admin can use powershell to remotely connect to any servers which are not physically in the same location and can run commands as required. The various administrative tasks can be performed remotely which saves hours of time for IT professionals. 

It is used to create scripts. The scripts are saved in .ps1 file format. These scripts are then used with the task scheduler to run the script at the desired time and without user intervention.

Example:  In the below script, we are saving a timestamp in a file. And used that file, to create a task.

Get-Date | Out-File C:\Users\Administrator\Documents\log.txt -Append

Get-PSDrive -PSProvider FileSystem | Out-File C:\Users\Administrator\Documents\log.txt -Append

The script is saved as log.ps1. 

In the below script, a task is created using the above file i.e. log.ps1

Now to verify, open the task scheduler, and check if the task is created or not. To open the task scheduler, click on Windows and search for the task scheduler.

This task will run on a system startup.

The task ran successfully.

From basic automation to advanced scripting, PowerShell can provide an excess of opportunities for simplifying tasks and saving time. PowerShell scripting is used to simplify many areas of an IT environment including server configuration and deployment, user creation and auditing, and administrative tasks.

Leave A Comment

Your email address will not be published. Required fields are marked *