Security Power Level: Advanced Chapter 17: PowerShell Security and Hardening
Execution policy and script signing
Execution policy sets the rules for which scripts may run, and signing adds a trusted mark to a script. Together they help you pause and check before you run code from someone else.
Lesson 1 of 4 in Chapter 17: PowerShell Security and Hardening. Previous step in this path: Reading Logs and Hardening Basics. 3 learning objectives, 5 examples, and a knowledge check.
Start: what you will learn
Execution policy sets the rules for which scripts may run, and signing adds a trusted mark to a script. Together they help you pause and check before you run code from someone else.
Learning objectives
- Explain execution policy levels in simple terms
- Check a script signature with Get-AuthenticodeSignature
- Explain why execution policy alone is not full protection
Concept in simple words
What it is: Execution policy is a setting that controls which PowerShell scripts are allowed to run on a computer. A script is a saved file of commands, usually ending in .ps1. When PowerShell loads a script file, it checks the policy first. If the policy does not allow that kind of script, PowerShell will not run the file. This check happens before the commands inside the file can do any work.
What signing adds: Signing a script means a trusted publisher adds a digital signature to it. Think of the signature as a sealed stamp plus a fingerprint of the file content. The stamp names the publisher through a certificate. The fingerprint lets PowerShell notice if the file content changed after it was signed. If the content changed, the fingerprint no longer matches, and the signature check will not come back as Valid.
Why it works
Why this exists: Scripts are powerful because one file can run many commands in seconds. That power is also the risk. A script you download, receive by email, or copy from a post can look helpful and still do something you do not want. Execution policy gives you a default rule so an unknown script does not just run because you double clicked it. Signing gives you extra evidence about the source, so you can judge whether that source is one you trust.
Why policy alone is not full protection: Execution policy controls script files and configuration files. It does not block every command you type by hand in a session. That is an important limit to understand. A policy is one safety rule, not a wall around the whole computer. It slows down careless script runs. It does not prove that a script is safe, and it does not replace reading the script or checking its source.
Think of it like this
Think of it like this: Execution policy is like a door rule at a building. The rule says which visitors may enter without extra checks. A signature is like an identity badge. A badge helps you trust the person at the door, but you still need to check what they plan to do once they are inside. A trusted badge on a person carrying the wrong tools for your job is still a problem, so you check the plan, not only the badge.
How the policy is decided behind the scenes: More than one rule can apply to the same computer. For example, a rule can apply to one user, and a different rule can apply to the whole local computer. Get-ExecutionPolicy shows the effective policy, which is the one rule that wins for your current session. If that result surprises you, Get-ExecutionPolicy -List shows the separate rules side by side, so you can see that more than one setting is involved. A managed policy from an administrator can also override local settings, which is why your result may differ from a friend result on another computer.
The levels in simple terms, one by one: Restricted means no script files run, while commands you type by hand still work. AllSigned means only signed scripts run, so every script file needs a signature that checks out. RemoteSigned means scripts you created on your own computer may run, but scripts from elsewhere must be signed. Unrestricted means scripts may run, with a prompt for scripts from elsewhere, so it is the loosest rule and it still asks you to pause for an outside script. Learn these four words well, because they are the whole vocabulary of this lesson.
How it works, step by step
How it works, step by step, in order: First, check the current policy with Get-ExecutionPolicy, which shows the rule in force for your session. Second, if the result is unclear, check Get-ExecutionPolicy -List to see the separate rules. Third, before you run a script, check its signature with Get-AuthenticodeSignature. Fourth, check that the signer name matches the publisher you expected. Fifth, read the whole script, slowly, and note every command that changes, deletes, stops, or sends something. Sixth, only then decide whether to run it. If any step gives you a surprise, stop and ask for help instead of pushing on.
Worked example 1, Get-ExecutionPolicy: In Get-ExecutionPolicy, Get-ExecutionPolicy is the command that asks for the rule in force, and it takes no file name and changes nothing, so it is a safe first check. Look for one word in the output, such as RemoteSigned. That word is the effective policy for your session. If you change nothing else, the same command on the same computer in the same session should give the same word. If you see Restricted, expect script files to be blocked, even if your typed commands still work.
Worked example 2, Get-ExecutionPolicy -List: In Get-ExecutionPolicy -List, -List asks for the list of rules instead of only the winning rule. Look for rows with a scope name and a policy word, such as CurrentUser and LocalMachine. Use this output when the simple result does not match what you expected. If you change -List to nothing, you go back to the single winning word. If you read only one row and ignore the rest, you may miss the rule that actually wins.
Worked example 3, Get-AuthenticodeSignature -FilePath .\Report.ps1: In Get-AuthenticodeSignature -FilePath .\Report.ps1, Get-AuthenticodeSignature is the command that checks a signature, -FilePath tells it which file to check, and .\Report.ps1 is the script in the current folder. Look first at Status. Valid means the signature checks out. NotSigned means the file has no signature at all. A Valid result tells you about the publisher, not about whether the script is safe for your job, so you still read it first. If you change the file path to a different script, you are checking a different file, and the old result tells you nothing about the new file.
How to read every Status result: Valid means the signature checks out and the certificate chain is trusted on that computer. NotSigned means there is no signature to check. HashMismatch means the file content no longer matches the signed content, so treat the file as changed after signing and do not run it. NotTrusted means the signer chain does not reach a trusted root on that computer, so stop unless your own team policy clearly explains that signer. UnknownError means PowerShell could not validate the signature, so treat the file as unverified. When in doubt, the safe action is the same in every case: stop, ask where the file came from, and read it before you decide anything else.
Worked example 4, Get-AuthenticodeSignature with Select-Object: In Get-AuthenticodeSignature -FilePath .\Report.ps1 | Select-Object -Property Status, Path, the first part checks the signature, the pipe character sends that result onward, and Select-Object -Property Status, Path keeps only the Status and Path columns. Look for a short table with those two columns, which is easier to read than the full result. If you change Status to SignerCertificate, you will see certificate detail instead of the simple status word. If you remove Select-Object, you get the full result again, which is fine, just longer to read.
Worked example 5, reading only the Status value: In (Get-AuthenticodeSignature -FilePath .\Report.ps1).Status, the round brackets run the signature check first, and .Status reads only the Status property from that result. Look for one word, such as Valid or NotSigned. This form is useful when you want a quick test in a script, because one word is easy to compare. If you change .Status to .Path, you get the file path instead, which answers a different question. If you forget the brackets, PowerShell will not know which result you mean, so keep the brackets in place.
What changes if you change one part: Change the file path and you check a different file, so never reuse a Valid result for a new file. Change RemoteSigned to AllSigned in your mental model and the rule gets stricter, because now even your own local scripts need a signature. Change Valid to NotSigned in your reading and the meaning flips completely, from checked signature to no signature at all. Change the signer name you expected to a different name and you should stop, because a valid signature from an unexpected publisher is not the same as a valid signature from the publisher you wanted.
Edge cases that catch beginners: A script created on your own computer can run under RemoteSigned without a signature, but running is not the same as safe, so read it anyway, especially if you copied it from somewhere else and then saved it locally. A signed script can still be wrong for your job, for example if it stops a service you need or deletes files you wanted to keep. A signature check can also fail for a boring reason, such as a changed file or an untrusted chain, and a boring reason is still a reason to stop. Never turn a failed check into a reason to try harder to run the file.
When to use it
When to use it, and when not to: Use a stricter policy on a computer you manage for other people, and use signing when you share scripts with a team, because both habits make the source and the rule visible. Do not treat policy as a wall that stops all bad code, and do not run a signed script without reading it. Changing a policy is an administrator decision for a managed computer, and this lesson teaches you to check and understand the rule, not to change it on your own. If a policy blocks your task, that block is information, not an error to work around.
A reading checklist before you run any script: Read from the top to the bottom, not only the first lines. Note every command that stops, removes, sets, creates, or sends something. Note every file path, so you know what will be touched. Note every service name, so you know what will be stopped or started. If a line is unclear, look it up or ask before you run it. If the script is long, read it in chunks and write a one line note for each chunk about what it does. This slow reading is the real safety step, and the signature check is only the step before it.
How to spot a mistake in your own thinking: If you catch yourself saying the script must be safe because it is signed, pause, because signing proves origin, not safety for your job. If you catch yourself saying the policy will protect you, pause, because policy is only one rule and typed commands still run. If you catch yourself skipping the signature check because the file came from a friend, pause, because a friend can forward a changed file without knowing it. Each pause is a good sign, because it means the habit is working.
Worked examples
Read each example in order. Each one explains its parts, then shows what to look for in the output.
Syntax
Get-AuthenticodeSignature -FilePath <path>Example 1
Get-ExecutionPolicy shows the execution policy in force. It takes no file name and changes nothing, so it is a safe first check. Look for one policy word in the output.
Get-ExecutionPolicyIllustrative output: RemoteSigned
Example 2
Get-ExecutionPolicy shows the policy, and -List asks for the separate rules instead of only the winning rule. Look for scope rows such as CurrentUser and LocalMachine, and use this list when the simple result surprises you.
Get-ExecutionPolicy -ListIllustrative output: Scope ExecutionPolicy ----- --------------- CurrentUser RemoteSigned LocalMachine AllSigned
Example 3
Get-AuthenticodeSignature checks the signature on one file. The -FilePath parameter names the script to check, and the Status in the result tells you whether a valid signature is present. A Valid result tells you about origin, not about safety for your job.
Get-AuthenticodeSignature -FilePath .\Report.ps1Illustrative output: Status SignerCertificateSubject ------ ------------------------ Valid CN=Example Publisher
Example 4
The first part checks the signature, the pipe sends that result onward, and Select-Object -Property Status, Path keeps only those two columns. Use this shorter form when the full result is hard to read.
Get-AuthenticodeSignature -FilePath .\Report.ps1 | Select-Object -Property Status, PathIllustrative output: Status Path ------ ---- Valid .\Report.ps1
Example 5
The round brackets run the signature check first, and .Status reads only the Status property from that result. Look for one word such as Valid or NotSigned, which is easy to test in a script.
(Get-AuthenticodeSignature -FilePath .\Report.ps1).StatusIllustrative output: Valid
Nothing on this page executes PowerShell code. The outputs above are illustrative only.
Common mistakes and troubleshooting
Read these before you try the examples, so you can spot the same problems in your own work.
- Running a script just because it is signed, without reading what it does first.
- Treating execution policy as complete protection, when it is only one safety rule among several.
- Ignoring a NotSigned result and running the script anyway, instead of asking where it came from and reading it first.
- Thinking Restricted blocks everything, when typed commands still run and only script files are blocked by that policy.
- Trusting a Valid result without checking that the signer name matches the publisher you actually expected.
- Treating HashMismatch or NotTrusted as a small warning, when both mean stop and check the file source before you decide anything else.
- Assuming a local script under RemoteSigned is automatically safe, when a copied script saved locally still needs to be read first.
Knowledge check
Knowledge check
What does a Valid result from Get-AuthenticodeSignature tell you?
Pick one answer. Feedback appears right away, and you can try again.
Remember this and next step
Remember this: Policy sets the running rules for script files, signing shows the publisher and whether the file changed after signing, and you should always check the policy, check the signature, check that the signer matches the publisher you expected, and read the script before you run it. A Valid result is useful evidence about origin. It is never, on its own, proof that a script is right or safe for your task.
Next step: Constrained language mode