PowerShell ExecutionPolicy Explained

-

|

What is PowerShell ExecutionPolicy?

ExecutionPolicy in PowerShell is a security feature that controls how PowerShell loads configuration files and runs scripts. ExecutionPolicy features helps prevent PowerShell running malicious scripts.

You can set Execution Policy for a computer, a user or a session. The Set-ExecutionPolicy cmdlet can be used to set ExecutionPolicy. PowerShell.exe command also has an ExecutionPolicy parameter that you can use to set Powershell ExecutionPolicy.

In this guide you will learn about the different types of PowerShell Execution Policies. You will also learn how to use Set-ExecutionPolicy and PowerShell.exe -ExecutionPolicy commands.

Available Execution Policies

This section explains the different ExecutionPolicy you can set in PowerShell and what each will allow you to do.

Restricted

  • This is the default execution policy in Windows 8, Server 2012 and above.
  • Prevents PowerShell configuration files (.ps1xml), module script files (.psm1), and Windows PowerShell profiles (.ps1) from executing
  • Allows individual PowerShell commands but denies scripts

AllSigned

When you set AllSigned ExecutionPolicy, PowerShell:

  • Runs all scripts
  • Requires that before a script is permitted to run it must be signed by a trusted publisher. This includes scripts written on the local computer
  • Prompts to confirm before running a script from a publisher you have not confirmed is trusted
  • May run signed but malicious codes

RemoteSigned

RemoteSigned ExecutionPolicy:

  • Allows scripts to run
  • Requires that all scripts downloaded from the Internet must be digitally signed by a publisher you specified as trusted. This includes scripts received via email and instant messaging platforms.
  • Will not require digital signing of scripts written on a local computer
  • May allow running of malicious scripts from other sources

Unrestricted

  • Allows unassigned scripts to execute
  • Will warn you before executing a script from the internet
  • Risks running malicious codes or scripts

Bypass

With Bypass ExecutionPolicy:

  • No script is blocked. Does not offer warnings as well.

Undefined

  • Means no ExecutionPolicy is defined
  • The effective ExecutionPolicy is Restricted (default)

ExecutionPolicy Scope and Precedence

You can apply PowerShell Execution Policy to:

LocalMcachine: Affects only the current users. This has the least precedence

CurrentUser: The Execution Policy affects only the current user. Takes precedence over LocalMcachine but has a lower precedence over Process.

Process: Applies only to the current session. Takes the highest precedence.

How to Get Current PowerShell ExecutionPolicy

To see the current PowerShell ExecutionPolicy run the command below:

Get-ExecutionPolicy 
How to Get Current PowerShell ExecutionPolicy - Get-ExecutionPolicy

To see the ExecutionPolicy set for all Scopes use this command:

Get-ExecutionPolicy -List
How to Get Current PowerShell ExecutionPolicy: Get-ExecutionPolicy -List

You can also get Execution Policy based on scope. Here are some sample commands:

Get-ExecutionPolicy -Scope CurrentUser
Get-ExecutionPolicy -Scope LocalMachine
Get-ExecutionPolicy -Scope Process

How to Set PowerShell ExecutionPolicy

As I mentioned in the introduction, you can set ExecutionPolicy with the Set-ExecutionPolicy cmdlet. You can also use the PowerShell.exe -ExecutionPolicy command.

How to Set Execution Policy with Set-ExecutionPolicy

The syntax of the Set-ExecutionPolicy cmdlet is:

Set-ExecutionPolicy -ExecutionPolicy <ExecutionPolicy>

As an example, to set the Execution Policy to RemoteSigned use the command below:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

Here is the result of the command.

How to Set Execution Policy with Set-ExecutionPolicy
The command failed because I did not open PowerShell as Administrator.

Here is the command executed as administrator:

How to Set Execution Policy with Set-ExecutionPolicy

As you can see from the image, the new Execution Policy is now RemoteSigned.

From the last 2 commands, I was asked to confirm the command. To bypass this prompt, include the -Force parameter.

The command now executes without prompting for confirmation.

The -Force parameter is particularly useful in scripting. Without this parameter your script may halt.

By default this command sets execution policy for LocalMachine scope. To confirm this, run the command below:

Get-ExecutionPolicy -List
How to Set Execution Policy with Set-ExecutionPolicy

To set Execution Policy to apply to another scope, use the –Scope parameter.

The command below sets execution policy for CurrentUser:

Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser -Force

As you can see from the result, CurrentUser policy is no longer Undefined. It is now Bypass, the policy set with the last command.

How to Set ExecutionPolicy with PowerShell.exe -ExecutionPolicy

If you want to bypass Execution Policy for the logged in session, you can set Execution Policy for the current command line. This is achieved using the PowerShell.exe command.

Before I give examples, here is the current execution policy:

The current execution policy is Restricted. This is because the Process scope takes precedence. To confirm, see the result below:

If you recollect, Restricted policy will NOT allow scripts to run.

This means that if I try to run a script, I will be denied access. The image below shows a series of commands and their results. See my explanation beneath the image.

How to Set Execution Policy with PowerShell.exe -ExecutionPolicy

The first command shown in the image is:

powershell.exe -file “C:\PS\schedule powershell example\Create-folders-from-text-file.ps1”

I tried to run a PowerShell script. Then I received the error message below:

“File C:\PS\schedule powershell example\Create-folders-from-text-file.ps1 cannot be loaded because running scripts is disabled on this system.”

The reason is because the current execution policy is Restricted. It does not allow any scripts to run.

Back to the reference image. In the next line, I ran the following command:

powershell.exe -file “C:\PS\schedule powershell example\Create-folders-from-text-file.ps1” -ExecutionPolicy Unrestricted

Effectively, I can set a temp execution policy for the command line using the -ExecutionPolicy parameter of the powershell.exe command. But I received the same error message. The reason is that I need to set the execution policy before I call the script.

Then in the next line, I executed this command:

powershell.exe -ExecutionPolicy Unrestricted

This sets the execution policy for the current logged in session to Unrestricted. So when I now run my PowerShell script, it executed successfully! See the reference image above.

The take from the last analysis is this: You can set Powershell ExecutionPolicy for the current session using Powershell.exe -ExecutionPolicy. But, you have to run the command first before calling your script.

To confirm that Powershell.exe only sets policy for the current session, I will run the command below from the current session:

Get-ExecutionPolicy -List

The result is Unrestricted for the CurrentUser scope. If I log off my current session and log back on. It will revert back to the policy set by Set-ExecutionPolicy command.

Conclusion

I hope I have been able to simplify how to get and set PowerShell Execution Policies. If you have any question or comments use the “Leave a Reply” form at the end of the page.

Other Helpful Guides

Additional Resources and References

  • Was this post Helpful?
  • YesNo

LEAVE A REPLY

Please enter your comment!
Please enter your name here

ADVERTISEMENTS

FEATURED POSTS

How to Share Amazon Wish List

How to Share Amazon Wish List

This Itechguide teaches you how to share Amazon Wish List. The guide covers steps to share Amazon Wish List from a PC...
How to Pay with PayPal on Amazon

How to Pay with PayPal on Amazon

This Itechguide teaches you how to pay with PayPal on amazon. The guide covers 2 options to pay with PayPal on amazon....
How to Export Contacts from Outlook

How to Export Contacts from Outlook

This Itechguide teaches you how to export contacts from Outlook. The guide covers steps to export contacts from Outlook (Windows 10) or...
Best 90s Movies on Netflix

The 10 Best 90s Movies on Netflix

This Itechguide reviews the 10 best 90s movies on Netflix. The movies are listed in reverse order – with the 10th movie...
How to Clear Search History on Instagram

How to Clear Search History on Instagram

This Itechguide teaches you how to clear search history on Instagram. The guide covers steps to clear search history on Instagram from...

AMAZON DEALS

TRENDING POSTS

Remote Desktop Connection

Remote Desktop Connection an Internal Error Has Occurred [Fixed]

Introduction I recently received the error message "Remote Desktop Connection an Internal Error Has Occurred". It was strange because...

Find My Samsung: Register and Use Samsung Find my Mobile

Introduction Ever wondered how you could find your Samsung phone if you lost it? Find my Samsung or Samsung...
What is the Difference Between PowerShell and CMD?

Windows Powershell vs CMD: Differences and Similarities

Introduction This short guide compares Windows PowerShell vs CMD (Windows command prompt). I will cover the history and nature...
Spotify No Longer Supports this Version of Microsoft Edge

Spotify No Longer Supports this Version of Microsoft Edge [Fixed]

Introduction When you open Spotify web player on Microsoft Edge, you may receive the error message "Spotify No Longer...
Windows 10 Won't Boot

Windows 10 Won’t Boot With Black Screen? 3 Ways to Fix It

Why Won't Windows 10 Boot Up? If your Windows 10 stops with a black screen, the first question in...

ADVERTISEMENTS

BEST OF ITECHGUIDES

How to Hide Rows in Google Sheets

How to Hide Rows in Google Sheets

Introduction This guide demos how to hide rows in Google Sheets. The guide has two sections:
Excel Count

Excel Count: How to Count in Excel With Examples

Introduction Excel COUNT Function is used for counting items in a worksheet. Excel COUNT also has the conditional function,...
0xc000021a: Status System Process Terminated Error [Fixed]

0xc000021a: Status System Process Terminated Error [Fixed]

Introduction You may receive "0xc000021a (STATUS_SYSTEM_PROCESS_TERMINATED)" error after installing a new device on your Windows computer. You may also...
Windows 10 Network Discovery Keeps Turning Off

How to Fix Network Discovery If it Keeps Turning Off in Windows 10

Does your Windows 10 network discovery keep turning off? It is likely that one of its dependent services is not started.
How to Share Amazon Wish List

How to Share Amazon Wish List

This Itechguide teaches you how to share Amazon Wish List. The guide covers steps to share Amazon Wish List from a PC...

RECENT POSTS

How to Search for a Word in Google Docs

How to Search for a Word in Google Docs

This Itechguide teaches you how to search for a word in Google Docs. The guide covers steps to search for a word...
How to Create a Folder in Google Docs

How to Create a Folder in Google Docs

This Itechguide teaches you how to create a folder in Google Docs. The guide covers steps to create a folder in Google...
How to Check Amazon Gift Card Balance

How to Check Amazon Gift Card Balance

This Itechguide teaches you how to check Amazon gift card balance. The guide covers steps to check Amazon gift card balance from...
How to Remove Google Account from Phone

How to Remove Google Account from Phone

This Itechguide teaches you how to remove Google Account from phone. The guide covers steps to remove Google Account from Android or...
How to Watch Instagram Live on PC

How to Watch Instagram Live on PC

This Itechguide teaches you how to watch Instagram live on PC. The guide covers 2 steps to watch Instagram live on PC....

AMAZON DEALS

MUST READ

Windows defender blocked by Group Policy

Windows Defender Blocked by Group Policy [Fixed]

Introduction If you receive the error message Windows Defender is blocked by group policy, your computer may have been...
repair windows server 2016

2 Easy Options to Repair Windows Server 2016

Introduction This guide offers 2 quick ways to repair Windows server 2016. The method you use will depend on...
How to Tag Someone on Instagram

How to Tag Someone on Instagram

This Itechguide teaches you how to tag someone on Instagram. The guide covers steps to tag someone on Instagram from Android or...
How to Change Default Font in Google Docs

How to Change Default Font in Google Docs

This Itechguide teaches you how to change default font in Google Docs. Here are the high-level steps to change default font in...
5 Easy Ways to Open Command Prompt in Windows 10

5 Easy Ways to Open Command Prompt in Windows 10

Introduction This guide demos 5 methods to open command prompt in Windows 10. Options to Open...

AMAZON DEALS

By using this website you agree to accept our Privacy Policy and Terms & Conditions