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:
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:
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”:
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”:
Execute the code below to verify the change of the execution policy:
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”:
Run this code to verify the change in the execution policy:
Example 5: Set the Execution Policy of “Process” Scope to the “Restricted”
This instance will set the “Restricted” execution policy for the “Process” scope:
Execute the mentioned code below to check whether the execution policy change to “Restricted” or not:
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:
The stated execution policy has been enabled.
Verify the change of the execution policy for the 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”:
Execute the below code to verify the change in the execution policy:
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.