Home  >  Article  >  Computer Tutorials  >  How to automate tasks using PowerShell

How to automate tasks using PowerShell

PHPz
PHPzforward
2024-02-20 13:51:08950browse

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:mryunwei.com. If there is any infringement, please contact admin@php.cn delete