Skip to content

PowerShell Quick Reference ​

Working notes for the commands I repeat. Each section is a task, with the command ready to copy.

Run a .ps1 with the execution policy bypassed ​

From cmd (run the script from its own folder, or give the full path):

bat
powershell -NoProfile -ExecutionPolicy Bypass -File "C:\path\to\script.ps1"

From PowerShell:

powershell
powershell -NoProfile -ExecutionPolicy Bypass -File .\script.ps1
  • -NoProfile skips your profile, so the run is the same on every machine.
  • -ExecutionPolicy Bypass applies to this one process only. Nothing is changed on the machine, and no admin rights are needed.
  • Quote paths that contain spaces, such as "C:\Users\me\My Scripts\run.ps1".

Pass parameters ​

Put the parameters after the script path:

bat
powershell -NoProfile -ExecutionPolicy Bypass -File ".\Get-LongPaths.ps1" -RootPath "D:\Data" -Threshold 248

Check the exit code ​

bat
powershell -NoProfile -ExecutionPolicy Bypass -File .\script.ps1
echo exit code %ERRORLEVEL%

From inside PowerShell, use $LASTEXITCODE instead.

Set the bypass for the current PowerShell session only ​

Use this when you'll run several scripts in one window:

powershell
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
.\script.ps1

It resets when the window closes.

Unblock a downloaded script ​

Files from the internet or a zip carry a "Mark of the Web" flag, which can stop a script from running even with the bypass. Remove it with:

powershell
Unblock-File -Path .\script.ps1

To unblock every script in a folder:

powershell
Get-ChildItem -Path .\ -Filter *.ps1 | Unblock-File

Check the execution policy ​

powershell
Get-ExecutionPolicy -List

If a policy is set by Group Policy (the MachinePolicy or UserPolicy scope), the -ExecutionPolicy switch on the command line is ignored. Then the script runs only if the policy allows it, and the fix is in the Group Policy setting, not in the command.

Gotchas ​

  • -ExecutionPolicy Bypass removes the policy check only. Errors in the script, missing modules and permissions still stop it.
  • Running from Windows PowerShell 5.1 (powershell) and from PowerShell 7 (pwsh) use different profiles and modules. Use the same one each time.

IT field notes from STL Net Services.