Skip to content

How to Run a PowerShell script

PowerShell is a task automation and configuration management program from Microsoft. It allows you to run commands and scripts to get detailed information or configure settings. There are several methods and tools for running a script in PowerShell. In this article, you will learn how to run a PowerShell script step by step.

PowerShell Execution policies

The PowerShell execution policy is a safety feature that helps to prevent the execution of malicious scripts.

Before you run a PowerShell script, you need to check which execution policy is set. This is because the PowerShell has set the execution policy to Restricted by default, which means that it prevents you from running a script.

The table below shows the PowerShell execution policy.

Execution PolicyDescription
AllSignedYou can only run a script if it’s signed by a trusted publisher.
BypassYou can run all scripts because nothing is blocked, and there are no warnings or prompts.
DefaultThe default execution policy is Restricted for Windows clients and RemoteSigned for Windows Servers.
RemoteSignedIt requires a digital signature from a trusted publisher on a downloaded script. You can unblock a downloaded script and run it without a digital signature. This is the default execution policy for Windows servers.
RestrictedYou can’t run any PowerShell script. This is the default execution policy for Windows clients to prevent from running scripts.
UndefinedThere is no execution policy set, which means the execution policy is Restricted for Windows clients and RemoteSigned for Windows Servers.
UnrestrictedYou can run an unsigned script, but you will get a warning if it’s a downloaded script. This is the default execution policy for non-Windows computers which can’t be changed.

Get and set PowerShell execution policy

To get and set your PowerShell execution policy we need to start PowerShell as administrator.

  1. Click on the start menu
  2. Search for Windows PowerShell
  3. Right-click the Windows PowerShell app
  4. Select Run as administrator

To check which execution policy you have, you need to run the below PowerShell command.

Get-ExecutionPolicy

If you choose to set the execution policy to RemoteSigned, you can’t run a script without unblocking a downloaded script.

We recommend changing your execution policy to Unrestricted to run any script automatically. This way you can you can ignore unblocking a downloaded script.

Run the below PowerShell command to set your execution policy to Unrestricted.

Set-ExecutionPolicy Unrestricted

Now you can run a PowerShell script unrestricted.

Do not use Run with PowerShell

It’s not recommended to open the PowerShell script by right-clicking the script and selecting Run with PowerShell, as you can see in the below picture.

Right-click the script and click Run with PowerShell

It runs the script but once the script finishes, it will close the PowerShell window without showing any output result.

1. Run a PowerShell script in Terminal app

We will show you two options to open a PowerShell script in the Terminal app.

Option 1. To run a PowerShell script in the Terminal app, follow the below steps:

  1. Click on the start menu
  2. Search for Terminal
  3. Right-click on the app
  4. Click Run as administrator

Note: We always recommend opening a PowerShell script with Run as administrator.

Open Terminal app and Run as administrator

It will open your default PowerShell in the Terminal app. If you only have Windows PowerShell 5.1 installed, it’s best to install PowerShell 7 on Windows as well. Once you have them both installed, you can choose in which PowerShell version you want to run the script.

  1. Copy and paste the below PowerShell command
  2. Press Enter to run the script
C:\scripts\Get-Services.ps1

It should run the script in Terminal and show the output result.

Option 2. Open the PowerShell script in the Terminal app:

  1. Right-click in the folder in C:\scripts
  2. Select Open in Terminal
How to Run a PowerShell script Open in Terminal
  1. The default PowerShell opens, and you will land in C:\scripts
Terminal PowerShell 7 in scripts folder opens
  1. Copy and paste the below PowerShell command
  2. Press Enter to run the script
Get-Services.ps1

It should run the script in Terminal and show the output result.

Change PowerShell version in Terminal app

There are several ways to change to another PowerShell version in the Terminal app.

Method 1. Change the default PowerShell version in the Terminal app:

  1. Click on the dropdown arrow
  2. Select Settings
Change settings in Terminal app
  1. Change the Default profile by selecting from the dropdown arrow
  2. Click Save
Change the default profile in Terminal

Method 2. Change the PowerShell version in the Terminal app from the dropdown arrow:

  1. Click on the dropdown arrow
  2. Select PowerShell

It will open PowerShell 7 in a new window.

Change PowerShell version in Terminal

Method 3. Change the PowerShell version in the Terminal app by typing:

  1. Type powershell followed by Enter to open Windows Powershell 5.1
  2. Type pwsh followed by Enter to open PowerShell 7
Open PowerShell 5 or PowerShell 7 version in Terminal app

To close the Terminal app, you can:

  • Type exit followed by Enter to go back to the Command Prompt
  • Type exit again followed by Enter to close the Terminal

2. Run PowerShell script in Command Prompt (cmd)

To run a PowerShell script in the Command Prompt, follow the below steps:

  1. Click on the start menu
  2. Search for Command Prompt
  3. Right-click on the app
  4. Click Run as administrator
Open Command Prompt and Run as administrator
  1. Type powershell followed by Enter to open Windows Powershell 5.1
  2. Type pwsh followed by Enter to open PowerShell 7
Run PowerShell 5 or PowerShell 7 version in Command Prompt

The PowerShell version you typed in Command Prompt will open.

  1. Copy and paste the below script in Command Prompt
  2. Press Enter to run the script
C:\scripts\Get-Services.ps1

To close the Command Prompt, you can:

  1. Type exit followed by Enter to go back to the Command Prompt
  2. Type exit again followed by Enter to close the Command Prompt

3. Open in Windows PowerShell ISE

To run a PowerShell script in Windows PowerShell ISE, follow the below steps:

  1. Click on the start menu
  2. Search for Windows PowerShell ISE
  3. Right-click on the app
  4. Click Run as administrator
Open Windows PowerShell ISE and Run as administrator

Windows PowerShell ISE opens, and it shows a blue window.

  1. Copy and paste the below PowerShell command in the blue window
  2. Press Enter to run the PowerShell script
C:\scripts\Get-Services.ps1
How to run a PowerShell script in Windows PowerShell ISE

Change the window view of PowerShell ISE.

  1. Click on Show Script Pane Top
  2. Click the dropdown arrow
Show script pane top in Windows PowerShell ISE

Run the same PowerShell command in the top pane in Windows PowerShell ISE to get a better overview.

  1. Copy and paste the same PowerShell command in the top (white) pane
  2. Click Run Script
How to run a PowerShell script in Windows PowerShell ISE top pane

Another method is to run the entire PowerShell script in Windows PowerShell ISE.

  1. Copy the PowerShell script
  2. Paste it in Windows PowerShell ISE
  3. Click Run Script
Run a PowerShell script in Windows PowerShell ISE

You can run a selection of the PowerShell script in Windows PowerShell ISE.

  1. Copy and paste the PowerShell script into the top pane
  2. Select the commands you want to run
  3. Click Run Selection

Note: To run a part of a script, you need to select the commands and click Run Selection.

Run Selection of PowerShell script in Windows PowerShell ISE

4. Open in Visual Studio Code (VS Code)

To run a PowerShell script in Visual Studio Code, follow the below steps:

  1. Go to the start menu
  2. Search for VS Code
  3. Right-click and select Run as administrator
Open Visual Studio Code and Run as administrator
  1. Click File
  2. Select New Text File
Open New Text File in Visual Studio Code
  1. Click Select a language
Select a language in Visual Studio Code
  1. Type powershell in the search bar
  2. Click on PowerShell
Click on PowerShell in Visual Studio Code
  1. Copy and paste the below PowerShell command in the bottom pane
  2. Press Enter to run the PowerShell script
C:\scripts\Get-Services.ps1
Run PowerShell script in VS Code

You can also run the PowerShell script in the top pane in Visual Studio Code.

  1. Copy and paste the same PowerShell command in the top pane
  2. Click Run
Run PowerShell script in Visual Code Studio

Another method is to open the PowerShell script with Visual Studio Code.

  1. Go to the C:\scripts folder in File Explorer
  2. Double-click the .ps1 file

If you can’t find the Visual Studio Code, you can:

  1. Right-click the PowerShell script file
  2. Select Open with > Visual Studio Code
Open with Visual Studio Code
  1. You can also copy and paste the entire PowerShell script into the top pane in VS Code
  2. Click Run
Run entire PowerShell script in VS Code

You can run a selection of the PowerShell script in Visual Studio Code.

  1. Copy and paste the PowerShell script into the top pane
  2. Select the commands you want to run
  3. Click Run Selection

Note: To run a part of a script, you need to select the commands and click Run Selection.

Run selection of PowerShell script in Visual Studio Code.

That’s it!

Read more: Create unlimited Client Secret in Microsoft Entra ID »

Conclusion

You learned how to run a PowerShell script using several methods. Run the PowerShell script in the Command Prompt or Terminal app and decide which PowerShell version you want to run. In Windows PowerShell ISE or Visual Code, you can choose to run the entire PowerShell script or a selection of the script.

Did you enjoy this article? You may also like Assign Microsoft 365 licenses with group-based licensing. Don’t forget to follow us and share this article.

o365info Team

o365info Team

This article was written by our team of experienced IT architects, consultants, and engineers.

This Post Has 0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *