How to Run PowerShell Scripts Easily and Quickly

Whether you are a beginner who wants to understand the basics of PowerShell script execution or an expert looking for advanced tips, this article will guide you through the essential steps to run PowerShell scripts successfully.

PowerShell is a scripting language and command-line interface developed by Microsoft to ease system administration and task automation. PowerShell can be used to perform various operations, such as managing files, registry, processes, services, networks, and more.

PowerShell is based on the language.NET Framework and uses a syntax similar to the Visual Basic.NET programming language. PowerShell also has many features similar to the Python programming language. PowerShell also supports object-oriented programming, so you can create and manipulate objects that represent system resources.

win 11 powershell

A PowerShell script is a collection of PowerShell commands stored in a file. PowerShell scripts can be used for automation of repetitive tasks, or to perform complex tasks that would be difficult to do manually. PowerShell scripts typically have a.ps1 file extension and can be run from the PowerShell interface or Windows Explorer.

Set PowerShell Execution Policy

However, running PowerShell scripts on Windows 11 may not be as easy as you might think. By default, Windows 11 has an execution policy that restricts PowerShell scripts to keep the system secure. The execution policy determines whether you can run PowerShell scripts and from which source. There are four levels of execution policy you can choose from:

  • Restricted: This is the default level that disallows all PowerShell scripts. You can only run individual commands from the PowerShell interface.
  • AllSigned: You can run PowerShell scripts signed by trusted publishers. You will be asked to confirm before running the signed script.
  • RemoteSigned: You can run PowerShell scripts created or downloaded by yourself without a signature, but scripts that come from the Internet or other sources must be signed by a trusted publisher. You will also be asked to confirm before running the signed script.
  • Unrestricted: You can run all PowerShell scripts without restrictions. However, you’ll still get security warnings for scripts that come from the Internet or other sources.

To find out the current execution policy in Windows 11, you can open the PowerShell interface by pressing the keys (Windows + R) and typing “powershell”, then pressing Enter. Then, type the following command:

Get-ExecutionPolicy

If the output, the execution policy status is “Restricted”, meaning you can’t run PowerShell scripts at all. To change the execution policy, you need to open the PowerShell interface as an administrator by pressing the keys (Windows + X) and selecting Windows PowerShell (Admin) from the menu. Then, type the following command:

Set-ExecutionPolicy <level>

Where <level> is one of the four execution policy levels mentioned earlier.

Create a PowerShell script

To create a PowerShell script, you can use any text editor. This is a critical step that requires an understanding of PowerShell syntax. You may want to create a custom directory for your project and store scripts in it. Be sure to provide a descriptive and memorable file name.

Run PowerShell Scripts from the PowerShell Interface

The easiest way to run a PowerShell script is from the PowerShell interface itself. First of all, make sure that you are in the same directory as the PowerShell script that you want to run.

You can use the cd (change directory) command to switch directories. For example, if your PowerShell script is named “test.ps1” and is in the “C:\Scripts” folder, type the following command:

cd C:\Scripts

Then, to run the PowerShell script, type the following command:

.\test.ps1

Note that you need to add a period and slash before the PowerShell script name. This indicates that you want to run the PowerShell script from the current directory. If you don’t add one, PowerShell looks for PowerShell scripts in the system environment path, which may not contain your directory.

You can also add parameters (parameters) to PowerShell scripts if they support them. Parameters are additional arguments that can modify the behavior of PowerShell scripts. For example, if your PowerShell script is named “backup.ps1” and has “-path” and “-destination” parameters, you can run that script with the following command:

.\backup.ps1 -path C:\Data -destination D:\Backup

This command will run the “backup.ps1” script that will back up the “C:\Data” folder to the “D:\Backup” folder. You can find out what parameters a PowerShell script supports by opening it in a text editor and looking at the param section at the beginning of the script.

Latest Articles