search
HomeComputer TutorialsComputer KnowledgeHow to automate tasks using PowerShell

How to automate tasks using PowerShell

Feb 20, 2024 pm 01:51 PM
psgetshell script

If you are an IT administrator or technology expert, you must be aware of the importance of automation. Especially for Windows users, Microsoft PowerShell is one of the best automation tools. Microsoft offers a variety of tools for your automation needs, without the need to install third-party applications. This guide will detail how to leverage PowerShell to automate tasks.

What is a PowerShell script?

If you have experience using PowerShell, you may have used commands to configure your operating system. A script is a collection of these commands in a .ps1 file. .ps1 files contain scripts executed by PowerShell, such as the basic Get-Help command. While simple commands like Get-Help may not be worth saving to a .ps1 file, to save time, more complex commands like Get-Date-DisplayHint Date should be written into a script file. Later, we'll explore more complex commands that are worth saving in a .ps1 file.

How to use PowerShell to automate tasks

PowerShell is more than just an application, it is a scripting language built on the .Net CLR to automate IT tasks. It has backward compatibility with CMD and can automate simple or complex tasks. Due to its versatility, it can be used on Windows clients and servers, and in MacOS, Linux and cloud environments, PowerShell is a compelling tool used by most IT professionals to automate daily tasks.

To automate tasks in PowerShell, we just need to follow the steps below.

  • Configure execution policy in PowerShell to allow scripts to run
  • Create a PowerShell script
  • Run and test the PowerShell script
  • Scheduling scripts using Task Scheduler
  • Let’s talk about it in detail.

    1]Configure execution policy in PowerShell to allow scripts to run

    How to automate tasks using PowerShell

    Normally, Windows restricts users from running scripts in PowerShell and needs to be changed by executing specific commands. This restriction can be lifted with simple steps.

    To check the execution policy, run the following command in PowerShell’s elevated mode.

    获取-执行策略

    If it displays AllSigned, it means that all scripts and configuration files are signed by a trusted publisher, including scripts written on the local computer.

    To further change the execution policy, you can run the following command.

    设置执行策略不受限制

    or

    设置执行策略远程签名

    This will allow you to run PowerShell scripts.

    2]Create a PowerShell script

    How to automate tasks using PowerShell

    Exploring the world of PowerShell scripting would require a separate article, but here we’ll teach you how to create and run a basic PowerShell script.

    #获取所有计算机属性
    $system Info=Get-ComputerInfo
    $system Info
    
    #获取具体版本属性
    $versionInfo=Get-ComputerInfo-Property“*Version”
    $versionInfo

    This script retrieves all system and operating system properties, including version-related properties such as Windows version and BIOS version.

    Although there are several tools for writing PowerShell scripts, we will use Notepad, which comes preinstalled on your computer.

    So, open notepad, paste the script given earlier, go to File > Save As and give a name with extension .ps1, change the save as type to Delete All Files and press Enter . If your file doesn't save, change the location to your desktop and try again.

    Note: We recommend that you use Visual Studio Code to write PowerShell scripts, as Notepad lacks various features, making it not an ideal scripting utility.

    (3) Run and test the PowerShell script

    How to automate tasks using PowerShell

    Now, let’s see how to run the PowerShell script. Now that we have created the PowerShell script using Notepad, to run it we must use PowerShell. So, open PowerShell as administrator and run the following command.

    &“脚本位置/脚本名称.ps1”

    If you are not sure of the exact location of your script, right-click on it, go to Properties, and copy the exact location from the Location field. Just put the location and name of the script in the syntax given earlier and you're good to go.

    4] Schedule the script using Task Scheduler

    How to automate tasks using PowerShell

    If your script is running well, there is no harm in scheduling the script. In order to do this, we will use Task Scheduler, a built-in Windows application that, as the name suggests, does just that. Follow the steps mentioned below to arrange the PowerShell script.

  • Start task
  • Right-click the Task Scheduler library and click Create Basic Task.
  • Enter a name and description (if required) and click Next.
  • Now, select when you want the task to be scripted and click Next.
  • Select the correct date and time in the next window and click Next.
  • In the action window, select Start the program and click Next.
  • Then, in the Program/Script options, click Browse.
  • Navigate to C:WindowsSystem32WindowsPowerShellv1.0 and select powershell.exe.
  • Next, in Add parameters, enter the script name and location, and click Next>Finish.
  • How to automate tasks using PowerShell

    You can select the task and click Run to confirm that it is running.

    Hopefully you now know how to create and run PowerShell scripts on your Windows computer.

    Read: How to Delete Scheduled Tasks in Windows

    Can I use PowerShell to automate tasks?

    Yes, with the help of PowerShell scripting language, you can write scripts that, when executed, can automatically perform tasks for you. This script can be scheduled using the built-in task scheduler utility, and you're good to go.

    The above is the detailed content of How to automate tasks using PowerShell. For more information, please follow other related articles on the PHP Chinese website!

    Statement
    This article is reproduced at:每日运维. If there is any infringement, please contact admin@php.cn delete
    Fix the 'This Build of Vanguard Is out of Compliance” Error - MiniToolFix the 'This Build of Vanguard Is out of Compliance” Error - MiniToolApr 15, 2025 am 12:50 AM

    You may encounter the “this build of Vanguard is out of compliance” issue when attempting to launch Valorant on Windows 11. Why does the error message appear? How to get rid of the error message? This post from php.cn gives details.

    How to Download RTX 4050 Drivers on Windows 10/11?How to Download RTX 4050 Drivers on Windows 10/11?Apr 15, 2025 am 12:49 AM

    NVIDIA GeForce RTX 40 series GPU might not be a new thing to you. Compared with other top graphics cards, many of you may pay more attention to mid-end mainstream RTX 4050 for its acceptable price. Read through this guide from php.cn Website to get d

    KB2267602 Fails to Install: Here Is How to Fix It!KB2267602 Fails to Install: Here Is How to Fix It!Apr 15, 2025 am 12:48 AM

    KB2267602 is a protection or definition update for Windows Defender designed to fix vulnerabilities and threats in Windows. Some users reported that they were unable to install KB2267602. This post from php.cn introduces how to fix the “KB2267602 fai

    Two Ways to Reinstall Pre-installed Software in Windows 11Two Ways to Reinstall Pre-installed Software in Windows 11Apr 15, 2025 am 12:47 AM

    Do you know how to reinstall pre-installed software in Windows 11 if you need to do this? In this post, we will introduce two easy ways. In addition, if you want to recover files on your Windows computer, you can try php.cn Power Data Recovery.

    Fixed: Desktop and File Folders Do Not Refresh AutomaticallyFixed: Desktop and File Folders Do Not Refresh AutomaticallyApr 15, 2025 am 12:46 AM

    Windows Desktop or File Explorer or folder will automatically refresh itself when you make some changes to it. However, some Windows 11/10 users report that they encounter the “desktop and file folders do not refresh automatically” issue. This post f

    Fixed: There Is a Problem with This Windows Installer PackageFixed: There Is a Problem with This Windows Installer PackageApr 15, 2025 am 12:45 AM

    When you try to install a program on Windows 11/10, you may fail to install it and receive an error message - there is a problem with this Windows installer package. This post from php.cn helps you to fix it.

    Can Antivirus Scan Encrypted Files? Security vs. PrivacyCan Antivirus Scan Encrypted Files? Security vs. PrivacyApr 15, 2025 am 12:44 AM

    When you run antivirus software on your device, you likely expect it to scan all files and folders for viruses. However, one question arises: can antivirus scan encrypted files? This inquiry delves into the realm of security versus privacy. php.cn So

    Free Download or Update HDMI Video Drivers on a Windows PCFree Download or Update HDMI Video Drivers on a Windows PCApr 15, 2025 am 12:43 AM

    How to download HDMI video drivers on Windows? How to update HDMI drivers to the latest versions. You can find the ways here. In addition, you can try php.cn Power Data Recovery to get your lost and deleted files back if necessary.

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    WWE 2K25: How To Unlock Everything In MyRise
    1 months agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    PhpStorm Mac version

    PhpStorm Mac version

    The latest (2018.2.1) professional PHP integrated development tool

    SublimeText3 English version

    SublimeText3 English version

    Recommended: Win version, supports code prompts!

    WebStorm Mac version

    WebStorm Mac version

    Useful JavaScript development tools

    SAP NetWeaver Server Adapter for Eclipse

    SAP NetWeaver Server Adapter for Eclipse

    Integrate Eclipse with SAP NetWeaver application server.

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment