Some users want to change their mailbox's time zone, language, date format, or time format.…
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.
Table of contents
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 Policy | Description |
---|---|
AllSigned | You can only run a script if it’s signed by a trusted publisher. |
Bypass | You can run all scripts because nothing is blocked, and there are no warnings or prompts. |
Default | The default execution policy is Restricted for Windows clients and RemoteSigned for Windows Servers. |
RemoteSigned | It 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. |
Restricted | You can’t run any PowerShell script. This is the default execution policy for Windows clients to prevent from running scripts. |
Undefined | There is no execution policy set, which means the execution policy is Restricted for Windows clients and RemoteSigned for Windows Servers. |
Unrestricted | You 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.
- Click on the start menu
- Search for Windows PowerShell
- Right-click the Windows PowerShell app
- 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.
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:
- Click on the start menu
- Search for Terminal
- Right-click on the app
- Click Run as administrator
Note: We always recommend opening a PowerShell script with 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.
- Copy and paste the below PowerShell command
- 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:
- Right-click in the folder in C:\scripts
- Select Open in Terminal
- The default PowerShell opens, and you will land in C:\scripts
- Copy and paste the below PowerShell command
- 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:
- Click on the dropdown arrow
- Select Settings
- Change the Default profile by selecting from the dropdown arrow
- Click Save
Method 2. Change the PowerShell version in the Terminal app from the dropdown arrow:
- Click on the dropdown arrow
- Select PowerShell
It will open PowerShell 7 in a new window.
Method 3. Change the PowerShell version in the Terminal app by typing:
- Type powershell followed by Enter to open Windows Powershell 5.1
- Type pwsh followed by Enter to open PowerShell 7
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:
- Click on the start menu
- Search for Command Prompt
- Right-click on the app
- Click Run as administrator
- Type powershell followed by Enter to open Windows Powershell 5.1
- Type pwsh followed by Enter to open PowerShell 7
The PowerShell version you typed in Command Prompt will open.
- Copy and paste the below script in Command Prompt
- Press Enter to run the script
C:\scripts\Get-Services.ps1
To close the Command Prompt, you can:
- Type exit followed by Enter to go back to the Command Prompt
- 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:
- Click on the start menu
- Search for Windows PowerShell ISE
- Right-click on the app
- Click Run as administrator
Windows PowerShell ISE opens, and it shows a blue window.
- Copy and paste the below PowerShell command in the blue window
- Press Enter to run the PowerShell script
C:\scripts\Get-Services.ps1
Change the window view of PowerShell ISE.
- Click on Show Script Pane Top
- Click the dropdown arrow
Run the same PowerShell command in the top pane in Windows PowerShell ISE to get a better overview.
- Copy and paste the same PowerShell command in the top (white) pane
- Click Run Script
Another method is to run the entire PowerShell script in Windows PowerShell ISE.
- Copy the PowerShell script
- Paste it in Windows PowerShell ISE
- Click Run Script
You can run a selection of the PowerShell script in Windows PowerShell ISE.
- Copy and paste the PowerShell script into the top pane
- Select the commands you want to run
- Click Run Selection
Note: To run a part of a script, you need to select the commands and click Run Selection.
4. Open in Visual Studio Code (VS Code)
To run a PowerShell script in Visual Studio Code, follow the below steps:
- Go to the start menu
- Search for VS Code
- Right-click and select Run as administrator
- Click File
- Select New Text File
- Click Select a language
- Type powershell in the search bar
- Click on PowerShell
- Copy and paste the below PowerShell command in the bottom pane
- Press Enter to run the PowerShell script
C:\scripts\Get-Services.ps1
You can also run the PowerShell script in the top pane in Visual Studio Code.
- Copy and paste the same PowerShell command in the top pane
- Click Run
Another method is to open the PowerShell script with Visual Studio Code.
- Go to the C:\scripts folder in File Explorer
- Double-click the .ps1 file
If you can’t find the Visual Studio Code, you can:
- Right-click the PowerShell script file
- Select Open with > Visual Studio Code
- You can also copy and paste the entire PowerShell script into the top pane in VS Code
- Click Run
You can run a selection of the PowerShell script in Visual Studio Code.
- Copy and paste the PowerShell script into the top pane
- Select the commands you want to run
- Click Run Selection
Note: To run a part of a script, you need to select the commands and click Run Selection.
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.
This Post Has 0 Comments