PowerShell Execution Policies
An execution policy is a setting that controls which scripts are allowed to run. It is a guardrail for scripts, not a lock on the machine.
Read the current policy
Get-ExecutionPolicy prints the setting in force. On this Linux machine it prints Unrestricted.
Get-ExecutionPolicyRead the policy for every scope
Each scope holds its own setting. The list prints every scope with its setting, in scope order.
Get-ExecutionPolicy -List | ForEach-Object { "$($_.Scope): $($_.ExecutionPolicy)" }Changing the policy fails on this platform
The change is attempted for one process only, inside try and catch. It fails, and the catch prints the real message.
try { Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned -Force -ErrorAction Stop } catch { "Caught: " + $_.Exception.Message }
Get-ExecutionPolicyExample
$policy = Get-ExecutionPolicy
$policy
if ($policy -eq "Unrestricted") { "Scripts run without a signature check." } else { "Policy is not Unrestricted." }
$count = (Get-ExecutionPolicy -List).Count
"Scopes: $count"Key points
- Execution policy: a setting that controls which scripts are allowed to run.
- Unrestricted: every script runs, with no signature check.
- RemoteSigned: local scripts run freely, and scripts from elsewhere need a trusted signature.
- Restricted: no scripts run at all, though single commands still work.
- AllSigned: every script must carry a trusted signature before it runs.
- Use Get-ExecutionPolicy to read the current setting, and read the setting for one scope at a time.
In this example, the block reads the current policy and prints it, then states what that setting means in plain words. The last lines count the scopes that hold a setting.
Note
on this Linux machine the setting reads Unrestricted, and changing it is not supported, even for one process only.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
On this Linux machine, Get-ExecutionPolicy prints Unrestricted. What does Unrestricted mean?