PowerShell Basics Level: Beginner Chapter 5: Turn Commands Into Scripts You Can Reuse

Scripts and security: execution policy

Execution policy is a safety setting. It controls whether script files are allowed to run, and it helps stop accidental or malicious scripts, but it is not a security boundary on its own.

Lesson 1 of 2 in Chapter 5: Turn Commands Into Scripts You Can Reuse. Previous step in this path: Multiple lines in a console. 3 learning objectives, 7 examples, and a knowledge check.

Start: what you will learn

Execution policy is a safety setting. It controls whether script files are allowed to run, and it helps stop accidental or malicious scripts, but it is not a security boundary on its own.

Learning objectives

  • Explain what execution policy controls
  • Read the current policy with Get-ExecutionPolicy
  • Choose a sensible policy for learning and for production

Concept in simple words

What it is: a script is a plain text file that holds one or more PowerShell commands, saved with a .ps1 ending. You write the commands once, save the file, and then run the file whenever you need the same work again. Execution policy is a separate setting. It controls the conditions under which PowerShell loads configuration files and runs script files on your computer.

The problem it solves: typing the same commands again and again is slow, and every retype is a fresh chance to make a typing mistake. A saved script gives you the same commands in the same order every time. That steadiness matters. If a result looks wrong, you can read the file, fix one line, and run it again, instead of trying to remember what you typed last time.

What PowerShell does behind the command: when you run a script file, PowerShell reads the file from top to bottom and runs each command in order, just as if you had typed those commands yourself, one after another. Output appears in that same order. A variable set near the top of the file is available to later lines in the same run. That simple top to bottom order is why the order of your lines matters, and why a clear comment at the top helps the next reader, who may be you in a month, follow what the script is meant to do.

What execution policy does behind the setting: before a script file runs, PowerShell checks the execution policy that applies to your session. Depending on the policy, it may allow the file, block it, or require a trusted digital signature. A digital signature is a kind of seal from a publisher. It says who published the file, but it does not prove the code inside is harmless or correct. Execution policy also applies to configuration files, such as a profile file, not only to scripts you run by hand.

What execution policy does not do: it does not stop you from typing commands yourself at the prompt. Under the most restrictive policy, you can still type commands one by one, but script files will not run. It also does not scan code for danger, and it does not judge whether code is well written. Microsoft describes execution policy as defense in depth, not as a security boundary. In plain words, it is one helpful layer, not a wall you can hide behind.

The policy values, in plain words: Restricted blocks script files, while still letting you type individual commands. RemoteSigned allows scripts you wrote on your own computer, and requires scripts that came from elsewhere to be signed by a trusted publisher. AllSigned requires every script and configuration file to be signed by a trusted publisher, including scripts you wrote yourself. Unrestricted allows unsigned scripts, with a warning for scripts from outside your local network area. Undefined means no policy is set at that scope, so PowerShell falls back to the effective default for that computer. For learning, you do not need the loosest values. You need the least permissive setting that lets your own work run.

How PowerShell decides which script came from elsewhere: on Windows, a file downloaded from the internet is often marked as coming from the internet. RemoteSigned uses that mark to decide whether a signature is required. That mark is helpful, but it is not perfect. Some ways of getting a file may not add the mark, and a mark on its own says nothing about whether the code is safe. That is why reading the script still matters, even when the policy allows it to run.

Think of it like this

Think of it like this: a script is a recipe card, and running it is cooking from the card instead of inventing the meal from memory each time. Execution policy is like a house rule about which recipe cards may be used. Cards you wrote yourself are fine, while cards that arrived from outside need a trusted seal. A seal tells you who made the card. It does not tell you the meal will taste good, and it does not tell you the recipe is safe for you. You still read the card first.

Safe scripting habits, before you run anything: read the whole script first, even a short one, and make sure you understand every command in it. If a line looks strange, or uses a command you do not know, look it up before you run the file. Test on your own computer, with your own test data, before you use a script on anything important. Never run a script you do not understand just because someone told you to, and never run a script only because the execution policy allowed it. The policy is a checkpoint about origin. Your own reading is the real safety check.

How it works, step by step

How it works, step by step: first, read the script file from top to bottom, so you know what it will do. Next, check your current setting with Get-ExecutionPolicy, so you know where you stand. Then, only if you need to change it for learning, set it for your own user account only, with Set-ExecutionPolicy RemoteSigned -Scope CurrentUser. After that, run Get-ExecutionPolicy again to confirm the new value took effect. Finally, run your script by typing its path, such as .\first.ps1 from its folder, and read the output carefully.

Scopes, in plain words: a scope is simply a level at which a setting can apply. CurrentUser applies only to your account, so it is the right scope for learning on a shared computer. LocalMachine applies to everyone who uses the computer, and changing it needs an administrator window. If you leave the scope out, PowerShell uses LocalMachine by default, which is one reason beginners get an access error when they only meant to change their own setting. There is also a Process scope, which lasts only until the current PowerShell window closes. For learning, CurrentUser is the clear and lasting choice.

When your change does not seem to work: run Get-ExecutionPolicy -List and look at every scope. A Group Policy setting from an administrator can override a local setting, and in that case your change may be accepted but the effective policy stays the same. Do not fight that by trying looser and looser settings. A managed setting is there for a reason. Note what you see, and ask the person who manages the computer if you need a different setting for your learning.

A Windows note, and an edge case: execution policy is a Windows feature. On non-Windows computers, the default policy is Unrestricted and it cannot be changed in the same way, because those systems do not use Windows security zones. So do not assume the same commands and the same behaviour on every system. Always check the effective policy on the computer in front of you, instead of assuming it matches a guide.

Let us walk through an example: in Set-ExecutionPolicy RemoteSigned -Scope CurrentUser, Set-ExecutionPolicy is the command that changes the setting. RemoteSigned is the policy value, meaning local scripts are allowed and downloaded scripts must be signed. -Scope CurrentUser limits the change to your account, so you are not changing the whole computer. PowerShell may ask you to confirm the change. Confirm only if you meant to make it, then run Get-ExecutionPolicy again to confirm the new value.

Let us walk through a second example: in Get-ExecutionPolicy -List, Get-ExecutionPolicy reads the execution policy setting. -List asks for the policy at every scope, and the output shows each scope and its policy side by side. Look for the CurrentUser row first, because that is the row your learning change should have set. Then look at the other rows. If a policy row shows a different value than you expected, that row may explain why the effective policy is not what you set.

How to read a blocked script message: if PowerShell says a script cannot be loaded because running scripts is disabled, or because a file is not digitally signed, that is a policy message, not a typing mistake in your script. Stop and check the policy with Get-ExecutionPolicy and Get-ExecutionPolicy -List. If instead PowerShell says it cannot find the path, that is a location problem, and you are probably in the wrong folder or you mistyped the file name. Reading the first line of the message carefully saves you from fixing the wrong thing.

When to use it

When to use it, and when not to: use RemoteSigned for learning, because it is a sensible balance between convenience and care. Use a stricter setting, such as AllSigned, only where signed code is already the normal way of working, for example in a managed production setup with a signing process in place. Do not set the policy to Unrestricted just to make a problem go away, and do not leave a looser setting in place after you try it. A looser setting does not fix a script you do not understand. It only removes a checkpoint.

Reuse, done well: once a script works, keep it in a folder you can find again, give it a clear name that says what it does, and keep the comment at the top up to date when you change it. Share the file, not a screenshot of the file, so the next person runs the exact same commands. Small scripts, read before running, checked with Get-ExecutionPolicy, and saved with care, are the foundation of reliable automation.

Worked examples

Read each example in order. Each one explains its parts, then shows what to look for in the output.

Syntax

Syntax
Set-ExecutionPolicy <Policy> -Scope CurrentUser

Example 1

Shows the effective policy for your session. Check this before you wonder why a script will not run. The effective policy is the one that actually applies right now, after all scopes are taken into account.

Example 1
Get-ExecutionPolicy

Illustrative output: RemoteSigned

Example 2

Allows your own local scripts, and requires downloaded scripts to be signed. Scoped to your user only, so other accounts on the computer are not changed. PowerShell may ask you to confirm. Confirm only if you meant to make this change.

Example 2
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

Illustrative output: A confirmation prompt, then the new policy takes effect.

Example 3

Get-ExecutionPolicy reads the execution policy setting. -List asks for the policy at every scope, and a scope is simply a level at which a setting can apply, such as the current user or the local machine. The output shows each scope and its policy side by side, so you can see which one is actually in force. A row that says Undefined means no policy is set at that scope.

Example 3
Get-ExecutionPolicy -List

Illustrative output: Scope ExecutionPolicy ----- --------------- MachinePolicy Undefined UserPolicy Undefined Process Undefined CurrentUser RemoteSigned LocalMachine Undefined

Example 4

Shows only the policy set for your account. Use this when you want to check your own learning change without reading the whole list. -Scope here picks which scope to look at, and CurrentUser means your account only.

Example 4
Get-ExecutionPolicy -Scope CurrentUser

Illustrative output: RemoteSigned

Example 5

Reads a script file and shows its lines on screen, without running it. Get-Content is the read first step. Read every line, check every command, and only then run the file. If you do not understand a line, look it up before you run anything.

Example 5
Get-Content .\first.ps1

Illustrative output: # first.ps1: greet the user $name = "Izaz" Write-Output "Hello, $name"

Example 6

Runs a script from the current folder. The .\ part means the current folder, and it is required, because PowerShell does not run a script by its bare name alone. Under a blocking policy, this line gives a policy message instead of the greeting. That message is your clue to check Get-ExecutionPolicy, not to guess at a looser setting.

Example 6
.\first.ps1

Illustrative output: A policy message saying the script cannot be loaded, when the effective policy blocks script files.

Example 7

Typed commands still run even under the most restrictive policy. Get-Date asks PowerShell for the current date and time. If this works but a script file does not run, you have confirmed the problem is about script files and policy, not about PowerShell itself being broken.

Example 7
Get-Date

Illustrative output: A date and time, because typed commands still run under Restricted.

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.

  • Setting Unrestricted to make a problem go away, then leaving it that way. Use the least permissive policy that works.
  • Thinking execution policy protects you from malicious code you run on purpose. It does not. Only run scripts you trust and understand.
  • Opening a downloaded script and running it without reading it, just because the execution policy allowed it to run. The policy is a checkpoint about origin, not a promise that the code is safe or correct.
  • Leaving the scope out of Set-ExecutionPolicy, so PowerShell tries LocalMachine by default, and then being surprised by an administrator error. For learning, always write -Scope CurrentUser.
  • Double clicking a .ps1 file and expecting it to run. PowerShell does not run a script on a double click, and does not run it by its bare name alone. Open PowerShell, go to the folder, and run it by its path.
  • Treating a trusted signature as proof that code is harmless. A signature says who published the file. A signed script can still do harm, so read it first, every time.
  • Fighting a Group Policy override by trying looser settings again and again. If Get-ExecutionPolicy -List shows a managed policy in force, your local change will not win. Note what you see and ask the person who manages the computer.
  • Assuming execution policy behaves the same on a non-Windows computer. On non-Windows systems the policy cannot be changed in the same way. Check the effective policy on the computer in front of you.

Knowledge check

Knowledge check

What does RemoteSigned allow?

Pick one answer. Feedback appears right away, and you can try again.

Remember this and next step

Remember this: scripts save work and give steady results, and execution policy is a checkpoint, not a wall. Check the policy first, choose the least permissive setting that works, set it for your own account when you are learning, read every script before you run it, and only run scripts you trust and understand.

Next step: Your first script and data types