How to Use Execution Policy in PowerShell: Simple Guide
Use the
Set-ExecutionPolicy cmdlet in PowerShell to control script execution permissions. You can set policies like Restricted, RemoteSigned, or Unrestricted to allow or block scripts from running.Syntax
The basic syntax to set the execution policy is:
Set-ExecutionPolicy -ExecutionPolicy <PolicyName>: Sets the policy.-Scope <ScopeName>(optional): Defines where the policy applies (e.g., CurrentUser, LocalMachine).-Force(optional): Applies the change without asking for confirmation.
powershell
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser -Force
Example
This example sets the execution policy to RemoteSigned for the current user, allowing scripts created locally to run but requiring downloaded scripts to be signed.
powershell
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser -Force Get-ExecutionPolicy -Scope CurrentUser
Output
RemoteSigned
Common Pitfalls
Common mistakes include:
- Trying to set the policy without administrator rights when changing
LocalMachinescope. - Not specifying the scope and unintentionally changing the policy for all users.
- Ignoring the confirmation prompt without using
-Force. - Expecting the policy to allow scripts without understanding the difference between policies like
RestrictedandRemoteSigned.
powershell
## Wrong: Changing LocalMachine without admin rights
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
## Right: Change for current user without admin
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser -ForceQuick Reference
| Execution Policy | Description |
|---|---|
| Restricted | No scripts run. This is the default on Windows. |
| AllSigned | Only scripts signed by a trusted publisher run. |
| RemoteSigned | Locally created scripts run; downloaded scripts must be signed. |
| Unrestricted | All scripts run; warnings for downloaded scripts. |
| Bypass | No restrictions or warnings. |
| Undefined | No policy set in the scope. |
Key Takeaways
Use Set-ExecutionPolicy to control script permissions in PowerShell.
Specify the scope to avoid changing policies for all users unintentionally.
Use -Force to skip confirmation prompts when setting policies.
RemoteSigned is a safe default allowing local scripts but protecting from unsigned downloads.
Administrator rights are required to change policies at the LocalMachine scope.