PowerShell provides advanced options to control and manage all processes on your system. So, if you wish to automate any process or a bunch of them, PowerShell is the way to go.
Unlike Command Prompts, PowerShell uses cmdlets. These cmdlets are not standalone executables but process inputs from one pipeline and give output to another for the same object. So, you can use a number of cmdlets for the same object and have them execute as a single task. This property makes automating PowerShell scripts extremely effective.
If you want to know how you can automate PowerShell scripts, this article should provide you with all the necessary information.
How to Automate PowerShell Scripts?
Depending on your system, you may not be able to run PowerShell scripts on your computer. This is because PowerShell includes a setting called execution policy to protect your computer by imposing a particular level of restriction on such scripts. And most systems have this setting enabled by default.
So, before automating the scripts, you need to remove or lower the restriction. First, open PowerShell as admin and enter the following command:Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned
If you need to run unsigned scripts from the internet or external source, you need to use the following command instead:Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
Then, use one of the methods below to automate PowerShell Scripts on Windows:
Using Task Scheduler
The Task Scheduler graphical tool provides an easy way to automate PowerShell scripts. It is the best option if you want to schedule a script you downloaded or got from an external source and don’t have much idea on how to use PowerShell.
- Open Run.
- Type
taskschd.msc
and press Enter to open the Task Scheduler. - Right-click on Task Scheduler Library and select Create Task.
- On the General tab, give any name and description you want for the task to automate the scripts.
- Choose between Run only when user is logged on and Run whether user is logged on or not, depending on your necessity.
- If you want to use another user account to run the task,
- Click on Change User or Group > Advanced > Find Now.
- Select your account from the list and click OK > OK.
- Set other options, such as if you want admin privileges, whether to make the task Hidden or configure it for another OS version.
- Now, go to the Triggers tab and click New.
- Pick the schedule or instance when you want to run the script from the drop-down box next to Begin the task.
- If you picked On a schedule, you need to set the frequency and start date. Otherwise, for most options, you need to specify the user.
- Then, set the advanced settings if you want. Make sure to check Enabled if you do so.
- Click OK.
- You can always edit the Trigger by clicking on it and selecting Edit. You can also add another trigger for the task.
- Now, go to the Actions tab and select New.
- Type
powershell
orpowershell.exe
in the Program/script text box. - Type
-File “full path to the script”
next to Add arguments. For example,-File “D:\New\script.ps1”
- Hit OK.
- Go to Conditions and Settings tabs and specify the options there.
- Click OK to create the task.
Using PowerShell
If you are familiar with using PowerShell for scripting, the easiest method to automate any scripts is by actually creating a scheduled task through PowerShell itself.
First, we discuss the cmdlets you need to use to create such schedules. If you already know what they are, you can skip ahead to the main steps. Otherwise, you’ll need to go through these to understand how to execute the process.
New-ScheduledTaskTrigger
It creates a trigger object for specifying when to schedule the task. You need to assign this cmdlet to a variable to create a trigger object.
You can use the following attributes with this cmdlet to trigger a task at regular intervals:
-At
: Specifies the time when you want to run the task-Once
: Starts the task once For e.g.,$trigger = New-ScheduledTaskTrigger -Once -At 4am
-Daily
: Starts the task with daily. You can also use-DaysInterval #
along with-Daily
to run the task once every # days. For example,$trigger = New-ScheduledTaskTrigger -Daily -DaysInterval 5 -At 4am
creates a trigger to run a task once every 5 days.- -Weekly: Starts the task week. You can also use -WeeksInterval # along with -Weekly to run a task once every # weeks.
If you want to run the task at startup, you need to use the -AtStartup attribute. Similarly, you need to use -AtLogon to run tasks at user logon. You can use the -User attribute to specify the exact user account.
New-ScheduledTaskAction
It specifies which application you wish to run. You also need to create a task action object by assigning it to another value if you wish to register the task on your computer.
The main attributes you can use are:
-Execute
: Specifies the executable file you need to automate; in this case,powershell.exe
-Argument
: Specifies while file to open with the executable file above. It’s better to enter the full path of the script, else PowerShell searches for it on the working directory. For example,$action = New-ScheduledTaskAction -Execute “powershell.exe” -Argument “D:\New\script.ps1”
-WorkingDirectory
: Describe the working directory where your system runs the task. The default one is%WinDir%\System32
Register-ScheduledTask
It registers the task on your local computer. You need to use the objects you create using the cmdlets above in the arguments of this cmdlet to automate the PowerShell scripts. The necessary attributes are:
-TaskName
: Any name you want for the task.-Trigger
: Here, you attach the trigger object that you create usingNew-ScheduledTaskTrigger
-User
: Specify the user account to which the task applies. You can also specify a computer if you want to automate the scripts for a network-attached computer.-Action
: Here, you attach the task object that theNew-ScheduledTaskAction
cmdlet creates.
For example, Register-ScheduledTask -TaskName "PowerShell Script" -Trigger $Trigger -User $User -Action $Action
There are many additional cmdlets you can use for this purpose as well as attributes you can use with them. Since they are optional and not relevant in most cases, we haven’t included those here. But you can still visit their documentation on Microsoft’s platform if you want to learn more about them.
Finally, here’s how you can use these cmdlets to automate PowerShell scripts:
- Open Run by pressing Win + R.
- Type
powershell
and press Enter to open Windows PowerShell. If you want to use PowerShell core (6.x, 7.x versions), you need to use the Run commandpwsh
. - Create a trigger object using the
New-ScheduledTaskTrigger
cmdlet. - Then, Create a task object to run a PowerShell script.
- Finally, register the scheduled task.
- For example, you can enter the following cmdlets on PowerShell:
$Trigger = New-ScheduledTaskTrigger -Once -At 10am
$Action = New-ScheduledTaskAction -Execute “powershell.exe” -Argument “D:\New\script.ps1”
$User = "DESKTOP\User"
Register-ScheduledTask -TaskName "PowerShell Script" -Trigger $Trigger -User $User -Action $Action