Powershell

Set-ExecutionPolicy for Managing PowerShell Execution Policies

The “Set-ExecutionPolicy” cmdlet in PowerShell sets the execution policies for Windows. The execution policies decide whether configuration files are allowed to run or not. PowerShell’s default execution policy is “Restricted”. Moreover, the default scope is set to the “LocalMachine”. Changing execution policies requires PowerShell to run as an administrator.

This write-up will observe techniques to manage PowerShell execution policies.

How to Use the “Set-ExecutionPolicy” Cmdlet for Managing PowerShell Execution Policies?

PowerShell scopes are used to limit the variables where they can be utilized and accessed. There are five scopes in PowerShell including “MachinePolicy”, “UserPolicy”, “Process”, “CurrentUser”, or “LocalMachine”. Similarly, there are six execution policies in PowerShell, including “AllSigned”, “Bypass”, “RemoteSigned”, “Restricted”, “Undefined”, or “Unrestricted”.

To read more about PowerShell scopes and execution policies click on this link.

Example 1: Get the List of the Execution Policies and Their Scopes

This example will get the list of PowerShell scopes and the policies assigned to them:

Get-ExecutionPolicy -List

Example 2: Set the Execution Policy of “CurrentUser” Scope to the “AllSigned”

In this illustration, the execution policy “AllSigned” will be assigned to the “CurrentUser” scope:

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy AllSigned

In the above-stated code:

  • First, write the “Set-ExecutionPolicy” cmdlet, followed by the “-Scope” parameter and assign the “CurrentUser” scope to it.
  • After that, add the “-ExecutionPolicy” parameter and specify the “AllSigned” execution policy.
  • When asked for a prompt, type “A” and press the “Enter” button:

Verify whether the execution policy changed for the scope of “CurrentUser”:

Get-ExecutionPolicy -Scope CurrentUser

According to the above code:

  • First, write the cmdlet “Get-ExecutionPolicy” to get the execution policy.
  • After that, add the “-Scope” parameter and specify the “CurrentUser” scope:

Example 3: Set the Execution Policy of the “CurrentUser” Scope to the “Bypass”

In this illustration, the execution policy of the “CurrentUser” scope will be set to the “Bypass”:

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass

Execute the code below to verify the change of the execution policy:

Get-ExecutionPolicy -Scope CurrentUser

Example 4: Set the Execution Policy of “Process” Scope to the “RemoteSigned”

This example will change the execution policy of the scope “Process” to “RemoteSigned”:

Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned

Run this code to verify the change in the execution policy:

Get-ExecutionPolicy -Scope Process

Example 5: Set the Execution Policy of “Process” Scope to the “Restricted”

This instance will set the “Restricted” execution policy for the “Process” scope:

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Restricted

Execute the mentioned code below to check whether the execution policy change to “Restricted” or not:

Get-ExecutionPolicy -Scope Process

Example 6: Set the Execution Policy of “LocalMachine” Scope to the “Undefined”

In this example, the execution policy “Undefined” will be enabled for the “LocalMachine” scope:

Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy Undefined

The stated execution policy has been enabled.

Verify the change of the execution policy for the scope “LocalMachine”:

Get-ExecutionPolicy -Scope LocalMachine

Example 7: Set the Execution Policy of “LocalMachine” Scope to “Unrestricted”

This illustration will change the execution policy for the “LocalMachine” scope to “Unrestricted”:

Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy Unrestricted

Execute the below code to verify the change in the execution policy:

Get-ExecutionPolicy -Scope LocalMachine

That was all about setting execution policy in PowerShell.

Conclusion

The cmdlet “Set-ExecutionPolicy” is used to manage the execution policies for PowerShell. It uses additional parameters, such as “-scope” and “-ExecutionPolicy” to change the execution policies for PowerShell scopes. Changing execution policies help remove limitations applied while running PowerShell scripts, which does not allow the running of scripts. This blog has elaborated on the procedure to manage the execution policies in PowerShell.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.