6 min readRishi

Fixing 'PowerShell Script Not Digitally Signed' — The Right Way

Fixing 'PowerShell Script Not Digitally Signed' — The Right Way

You download or write a PowerShell script, try to run it, and get:

File script.ps1 cannot be loaded. The file script.ps1 is not digitally signed.

That is the execution policy doing the job it was configured for. The fix depends on why the file is untrusted, and on which PowerShell you launched. Changing the machine-wide policy because a thread said Unrestricted is how you turn a signing error into an audit finding.

Two shells, two defaults

Windows PowerShell 5.1 (powershell.exe) defaults to Restricted: no scripts, only interactive commands. PowerShell 7 (pwsh) on Windows defaults to RemoteSigned: local scripts run; files that arrived from the internet must be signed. On non-Windows, PowerShell 7 defaults to Unrestricted because those platforms do not carry Windows zone identifiers.

A CI job that calls powershell and not pwsh is on 5.1's default. A terminal that prints PSVersion 7.x with a file from a browser is RemoteSigned plus a Zone.Identifier. Same error text, different lever.

$PSVersionTable.PSVersion
Get-ExecutionPolicy -List

-List is the one that matters. Policy is five scopes, and the first defined value in this order wins: MachinePolicy, UserPolicy, Process, CurrentUser, LocalMachine. Group Policy beats everything you set in a terminal. Get-ExecutionPolicy without -List only prints the effective result, which is why people set CurrentUser and wonder why the server still blocks.

The about_Execution_Policies topic is the canonical list:

PolicyBehavior
RestrictedNo scripts allowed (Windows PowerShell 5.1 default)
AllSignedOnly signed scripts run, including local ones
RemoteSignedDownloaded scripts must be signed; local scripts run (PowerShell 7 default on Windows)
UnrestrictedAll scripts run; downloaded files still prompt
BypassNo restrictions, no warnings

Undefined in a scope means "look at the next scope." It is not a sixth policy you set as a goal.

Restricted says "running scripts is disabled on this system." RemoteSigned and AllSigned talk about signatures. Unblock-File on a Restricted 5.1 session changes nothing. Read the error text before you pick a fix.

The Zone.Identifier is why a local file looks remote

Windows marks files that arrived through a browser, Outlook, or a network share with an alternate data stream named Zone.Identifier. RemoteSigned treats that stream as "this came from the internet." The file on your desktop is still downloaded.

Get-Item .\script.ps1 -Stream Zone.Identifier -ErrorAction SilentlyContinue
Get-Content .\script.ps1 -Stream Zone.Identifier

ZoneId=3 is the internet zone. For a script you have read and trust:

Unblock-File -Path .\script.ps1

That removes the stream. It does not sign the file and it does not change policy. Under RemoteSigned the script then runs like any other local file. Under AllSigned, Unblock-File does nothing useful — the policy still demands a signature. Unblock a file you have inspected. Do not recursively Unblock-File an entire Downloads tree as a login script.

Change policy only at the scope you mean

For a development machine where you write .ps1 files yourself, CurrentUser + RemoteSigned is the usual destination:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

You should not need LocalMachine for that, and you should not need an elevated prompt. If Set-ExecutionPolicy fails because Group Policy owns it, stop. The MachinePolicy or UserPolicy row is the one to change with your desktop team, not with -Force.

For a one-off, persist nothing:

pwsh -ExecutionPolicy Bypass -File .\script.ps1

Or inside a session you already have:

Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process

Process dies with the window. That is the right scope for a build-agent step that must run one unsigned script and leave the machine alone. Bypass at LocalMachine on a jump box is the change you will be asked to explain six months later.

Signing is the production path

If the script ships to shared servers or lands in a way Windows will mark as internet-zone, sign it. Unblock-File does not travel with a re-download.

$cert = Get-ChildItem -Path Cert:\CurrentUser\My -CodeSigningCert |
    Select-Object -First 1

Set-AuthenticodeSignature -FilePath .\script.ps1 `
    -Certificate $cert `
    -TimestampServer "http://timestamp.digicert.com"

Get-AuthenticodeSignature -FilePath .\script.ps1

The timestamp server matters. Without it, the signature becomes invalid when the code-signing certificate expires, even if the script has not changed.

For local experiments only:

$cert = New-SelfSignedCertificate -Type CodeSigningCert `
    -Subject "CN=Dev Signing" `
    -CertStoreLocation Cert:\CurrentUser\My

A self-signed cert is trusted only where you have installed it. AllSigned on a fleet plus a cert you forgot to deploy replaces one error with another. Production signing uses a certificate the organization already trusts, issued for code signing, not checked into git.

Get-AuthenticodeSignature returning NotSigned after you thought you signed usually means a different path, or an editor that stripped the signature footer when it re-saved the file. Sign as the last step before you publish. If you are packaging reusable commands, put them in a module — PowerShell modules — and sign what you ship.

CI already bypasses — until it does not

GitHub-hosted runners and Azure DevOps PowerShell tasks typically run scripts without the Restricted default getting in the way. When a self-hosted agent does not:

# GitHub Actions
- name: Run script
  shell: pwsh
  run: |
    Set-ExecutionPolicy Bypass -Scope Process -Force
    .\deploy.ps1
# Azure DevOps
- task: PowerShell@2
  inputs:
    filePath: 'deploy.ps1'
    # Azure DevOps PowerShell tasks use Unrestricted by default

Pin pwsh vs powershell explicitly. A job that calls powershell .\deploy.ps1 on a Windows image with 5.1 Restricted fails in a way the same file never fails locally in PowerShell 7.

What not to do

Do not paste Set-ExecutionPolicy Unrestricted without -Scope. The default scope is LocalMachine, which requires elevation and changes the box for every user.

Do not disable execution policy via Group Policy for a whole OU because one laptop could not Unblock-File.

Do not "fix" AllSigned by switching a server to Bypass so an unsigned bootstrap can run, then forget to switch back.

Dot-sourcing iex (Invoke-WebRequest ...) is not a workaround you should be proud of. That is how unsigned network code runs in a session that would have blocked the file.

Execution policy is a seatbelt, not a security boundary. A user who can run powershell -ExecutionPolicy Bypass already bypassed it. That is not an argument for turning it off at Machine scope. It is an argument for signing what you deploy.

Troubleshooting sequence

  1. $PSVersionTable.PSVersion — 5.1 or 7, and which executable.
  2. Get-ExecutionPolicy -List — which scope is in force. If MachinePolicy is not Undefined, talk to whoever owns that GPO.
  3. Get-Item .\script.ps1 -Stream Zone.Identifier — if ZoneId=3 and policy is RemoteSigned, Unblock-File.
  4. Get-AuthenticodeSignature .\script.ps1 — AllSigned needs Valid.
  5. Confirm the full path you signed is the file you are running.
  6. If Group Policy is Restricted, stop changing CurrentUser. It will never win.
  7. For a one-time run, pwsh -ExecutionPolicy Bypass -File. Then go back to step 3 or 4 for a durable fix.

Use RemoteSigned on development machines. Use Unblock-File for a trusted download. Sign what you ship. Leave Bypass on the process, not on the server.

Keep reading

Newsletter

New posts, straight to your inbox

One email when a new post goes up. Double opt-in, unsubscribe anytime. Not a daily digest.

Comments

  • No comments yet. Be the first.