LLI Wiki — My Digital Knowledge Base

PowerShell execution policy: check, change and run scripts

Check and change the PowerShell execution policy per scope, unblock downloaded scripts and run scripts from a network share.

PowerShell's execution policy decides which scripts can run on a machine or for a user. This article shows how to check the policy for each scope, change it, unblock a downloaded script and run one script without changing the policy.

Check the current policy

Get-ExecutionPolicy -List

The policy is evaluated by scope, from the highest precedence (MachinePolicy) to the lowest (Process). MachinePolicy and UserPolicy are normally set by Group Policy.

Change the policy

Scope Applies to Example
Process The current session only Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
CurrentUser The current user Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force
LocalMachine All users (administrator rights required) Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy Bypass -Force
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force
Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy Bypass -Force

Unblock a downloaded script

A script downloaded from the internet carries a "Mark of the Web" and can be blocked even when the policy allows it. Remove the mark with:

Unblock-File -Path C:\Downloads\script1.ps1

Run one script without changing the policy

powershell.exe -NoProfile -ExecutionPolicy Bypass -File .\script.ps1

Set the policy with Group Policy

  1. Run gpedit.msc to open the Group Policy Editor.
  2. In the left tree, go to Local Computer Policy, Computer Configuration, Administrative Templates, Windows Components, Windows PowerShell.
  3. In the right pane, double-click Turn On Script Execution.
  4. In the dialog, choose Enabled, then select the policy to apply (for example, Allow all scripts).

The equivalent registry values are shown below. Import them only if Group Policy is not an option.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\PowerShell]
"EnableScripts"=dword:00000001
"ExecutionPolicy"="Bypass"

Scripts on a network share

Running a script from a UNC share can show a "Security warning" prompt asking whether to run it. Options:

Set-ExecutionPolicy Unrestricted -Scope CurrentUser

or run the script with the policy set for that process only:

powershell.exe -NoProfile -ExecutionPolicy Bypass -File .\script.ps1

Notes

Mastodon LLIMastodon SocialFosstodon