Tech News Today
  • Hardware
    • Motherboards
    • CPUs
    • Graphic Cards
    • RAM
    • SSDs
    • Computer Cases
    • Monitors
    • Peripherals
    • Power Supply Unit
    • PC Builds
    • Computer Tips
  • Software
  • Operating System
    • Windows
    • Mac
    • Linux
  • Gaming
  • Mobile
  • Console
  • More
    • Internet
    • Networking
    • Security
    • Buyer’s Guide
    • Gadgets
    • Laptops
    • Reviews
    • How To
    • News
Facebook Twitter Instagram
Tech News Today
  • Hardware
    • Motherboards
    • CPUs
    • Graphic Cards
    • RAM
    • SSDs
    • Computer Cases
    • Monitors
    • Peripherals
    • Power Supply Unit
    • PC Builds
    • Computer Tips
  • Software
  • Operating System
    • Windows
    • Mac
    • Linux
  • Gaming
  • Mobile
  • Console
  • More
    • Internet
    • Networking
    • Security
    • Buyer’s Guide
    • Gadgets
    • Laptops
    • Reviews
    • How To
    • News
Tech News Today
Home»Windows»How to Automate PowerShell Scripts

How to Automate PowerShell Scripts

Abhishek SilwalBy Abhishek SilwalDecember 8, 2022
automate-powershell-scripts

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

Set execution policy

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.

  1. Open Run.
  2. Type taskschd.msc and press Enter to open the Task Scheduler.
  3. Right-click on Task Scheduler Library and select Create Task.
    task-sheduler-create-task
  4. On the General tab, give any name and description you want for the task to automate the scripts.
  5. Choose between Run only when user is logged on and Run whether user is logged on or not, depending on your necessity.
    logon-info
  6. 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.
      user-OK
  7. Set other options, such as if you want admin privileges, whether to make the task Hidden or configure it for another OS version.
  8. Now, go to the Triggers tab and click New.
    triggers-new
  9. Pick the schedule or instance when you want to run the script from the drop-down box next to Begin the task.
  10. 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.
  11. Then, set the advanced settings if you want. Make sure to check Enabled if you do so.
  12. Click OK.
    trigger-OK
  13. You can always edit the Trigger by clicking on it and selecting Edit. You can also add another trigger for the task.
  14. Now, go to the Actions tab and select New.
  15. Type powershell or powershell.exe in the Program/script text box.
  16. Type -File “full path to the script” next to Add arguments. For example, -File “D:\New\script.ps1”
    action-task-scheduler
  17. Hit OK.
  18. Go to Conditions and Settings tabs and specify the options there.
  19. 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
    new-scheduled-trigger
  • -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.
    new-scheduled-trigger-daily-interval
  • -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”
    powershell-ps1-task-action
  • -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 using New-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 the New-ScheduledTaskAction cmdlet creates.

For example, Register-ScheduledTask -TaskName "PowerShell Script" -Trigger $Trigger -User $User -Action $Action

register-scheduled-task

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:

  1. Open Run by pressing Win + R.
  2. 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 command pwsh.
    powershell-run
  3. Create a trigger object using the New-ScheduledTaskTrigger cmdlet.
  4. Then, Create a task object to run a PowerShell script.
  5. Finally, register the scheduled task.
  6. 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
      schedule-powershell-script-powershell
how-to
Abhishek Silwal
  • LinkedIn

Abhishek Silwal is an Electronics Engineer and a technical writer at TechNewsToday. He specializes in troubleshooting a wide range of computer-related issues. His educational background in Electronics Engineering has given him a solid foundation in understanding of computers. He is also proficient in several programming languages and has worked on various robotics projects. Even in his early days, he used to tinker with various computer components, both hardware, and software, to satiate his curiosity. This experience has given him a breadth of experience that goes beyond his educational qualification. Abhishek has been writing articles on dealing with varieties of technical issues and performing specific tasks, especially on a Windows machine. He strives to create comprehensive guides on fixing many system and hardware issues and help others solve their problems. You can contact him at abhisheksilwal@technewtoday.com

Related Posts

destination folder access denied

4 Ways to Fix Destination Folder Access Denied

May 30, 2023
disable fullscreen optimizations

How to Disable Fullscreen Optimizations on Windows

May 29, 2023
please wait for the gpsvc

Fix: Please Wait for the GPSVC

May 28, 2023
0x80300024

6 Ways to Fix 0x80300024 Error in Windows

May 25, 2023
check powershell version

3 Easy Ways to Check PowerShell Version

May 24, 2023
d drive not showing up

D Drive Not Showing Up—10 Ways to Fix It

May 22, 2023
Add A Comment

Leave A Reply Cancel Reply

Latest Posts
Does-Printer-Ink-Dry-Out

Does Printer Ink Dry Out? How to Last It Longer?

May 30, 2023
dmg file

What is DMG File? How to Open It?

May 26, 2023
what is port 443

Port 443: Everything You Need to Know About It

May 18, 2023
You may also like
how-to-airprint-to-hp-printer

How to AirPrint to HP Printer

May 30, 2023
destination folder access denied

4 Ways to Fix Destination Folder Access Denied

May 30, 2023
how to change wifi password

How to Change Wi-Fi Password? Step-by-Step Guide

May 29, 2023
Recommended
Cookie Clicker Garden Guide

Cookie Clicker Garden Guide to Unlocking Every Seed

September 26, 2021
monitor no signal

Computer Turns On But Monitor Says No Signal (9 Ways To Fix)

November 10, 2022
Facebook Twitter Pinterest
  • Home
  • About Us
  • Our Team
  • Editorial Guidelines
  • Privacy Policy
  • Affiliate Disclosure
© 2023 TechNewsToday, editor@technewstoday.com | Tech Central Pvt. Ltd.

Type above and press Enter to search. Press Esc to cancel.