Skip to main content
SysAdmin Shell Scripting Essentials

PowerShell Run Script: Syntax Reference & Troubleshooting Guide

Run a script from PowerShell executes a .ps1 file using PowerShell.exe or pwsh after configuring execution policy, typically to automate Windows administration tasks.

PowerShell -noexit c:tempPowerShellPowerShellExampleScript.ps1

The default execution policy on most Windows systems is Restricted, which blocks all script execution. This error appears when you attempt to run a .ps1 file without adjusting the policy.

Why “Running scripts is disabled on this system” occurs

Symptom Root Cause Typical Environment
Error: “Cannot be loaded because running scripts is disabled on this system” ExecutionPolicy set to Restricted or AllSigned without proper signature Default Windows 10/11, Server 2016+
Right-click “Run with PowerShell” does nothing for a moment Script execution blocks briefly then closes due to no -NoExit flag Any Windows with Restricted policy
Double-click opens script in editor instead of running Windows does not associate .ps1 with PowerShell.exe for execution All Windows versions
Script runs but terminates immediately (output lost) No -NoExit or Read-Host pause Launching from File Explorer or CMD

How to fix “Running scripts is disabled on this system”

  1. Check the current execution policy

    Open PowerShell as Administrator (Start → type “PowerShell” → right-click → “Run as administrator”). Run:

    Get-ExecutionPolicy

    Output likely says Restricted or AllSigned. On Windows Server Core you may see RemoteSigned.

  2. Set execution policy to RemoteSigned (per machine)

    Set-ExecutionPolicy RemoteSigned -Scope LocalMachine -Force

    This allows local scripts (.ps1 files on your machine) to run without signing, while scripts from the internet must be signed or explicitly unblocked via Unblock-File. Restart PowerShell to apply.

  3. Run the script from PowerShell console with -NoExit

    PowerShell -NoExit -File "C:scriptsMyScript.ps1"

    The -NoExit flag keeps the console open after the script finishes, essential for seeing output or debugging errors.

  4. Run script as Administrator via Start-Process (from cmd or another script)

    Start-Process PowerShell -Verb runAs -ArgumentList "-NoExit -File C:scriptsAdminScript.ps1"

    Use this when the script requires elevated privileges (e.g., modifying registry, services). The UAC prompt appears.

  5. Run script from Command Prompt (cmd.exe) directly

    powershell.exe -ExecutionPolicy Bypass -NoExit -File C:scriptsScript.ps1

    -ExecutionPolicy Bypass overrides the system policy for this single execution. Useful for automated jobs where policy changes are undesired.

  6. Run script using Windows PowerShell ISE (for development and debugging)

    Open ISE (search “Windows PowerShell ISE”), press Ctrl+O to open the script file, then press F5 to run the entire script. Press F8 to run only selected lines. To stop a hung script, press Ctrl+Break or click the Stop button (red square).

See also  Change Password Linux: Syntax, Flags, Examples & Troubleshooting
Method Command / Action Best For
Console with -NoExit PowerShell -NoExit -File script.ps1 Quick testing, viewing output
Run as Admin (elevated) Start-Process PowerShell -Verb runAs -ArgumentList "-File script.ps1" Scripts requiring admin rights
From CMD powershell.exe -ExecutionPolicy Bypass -File script.ps1 Batch files, scheduled tasks
ISE Open .ps1, press F5 Development, debugging
Right-click “Run with PowerShell” Quick action from Explorer Simple scripts (output disappears unless modified)

Common Errors

  • File not found: Use absolute paths or relative paths from the current directory. In ISE, the working directory defaults to the script location; in console, it’s the current prompt directory. Fix: PowerShell -File "C:FullPathscript.ps1"
  • .ps1 not recognized as a command: Windows does not add .ps1 to PATHEXT. Always invoke via PowerShell -File or use .script.ps1 from within a PowerShell session.
  • Script runs but closes immediately: Add -NoExit or include Read-Host -Prompt "Press Enter to exit" at the end of the script.
  • Execution policy cannot be changed (Group Policy managed): If set by GPO, you cannot override with Set-ExecutionPolicy. Use -ExecutionPolicy Bypass at invocation instead.
  • Script requires modules not loaded: Add Import-Module <ModuleName> at the top of the script or run Install-Module first.
  • Running from Task Scheduler fails: Ensure the “Start in (optional)” field is set to the script directory, and use powershell.exe -ExecutionPolicy Bypass -File "path" as the program/script.

Useful Commands

Action CLI Command Key Flag Description
Run script with output stay PowerShell -NoExit -File script.ps1 -NoExit Keeps console open after execution
Run script elevated Start-Process PowerShell -Verb runAs -ArgumentList "-File script.ps1" -Verb runAs Starts PowerShell as Administrator
Set execution policy Set-ExecutionPolicy RemoteSigned -Scope LocalMachine -Scope Defines the scope for the policy change
Bypass policy temporarily powershell.exe -ExecutionPolicy Bypass -File script.ps1 -ExecutionPolicy Bypass Overrides policy for one run
Run from CMD and wait powershell.exe -Command "& '.script.ps1'" -Command Alternative to -File, useful with arguments
See also  Crontab Every 4 Hours — Verified Syntax, Flags & Troubleshooting

Performance Considerations

Performance when running a PowerShell script depends on execution policy overhead, startup time, and resource constraints. Use the -NoExit flag only if you need interactive debugging after script completion — omitting it reduces process lifetime and memory usage. For administrative scripts, Start-Process PowerShell -Verb runAs triggers a User Account Control (UAC) dialog; batch elevation with scheduled tasks avoids that delay.

  • Execution policy check: Running Set-ExecutionPolicy RemoteSigned once eliminates per‑invocation policy prompts, reducing startup latency by 100–200 ms.
  • Script path: Use absolute paths (e.g., PowerShell c:tempscript.ps1) to avoid search overhead relative to environment variables.
  • Session reuse: For frequent runs, launch a persistent PowerShell v6+ session with -NoExit and pipe commands via stdin, avoiding process spawn cost.
  • Parallelism: On PowerShell 5.0+, use workflows; for high throughput, batch scripts using Read-Host loops with single prompt.
# Fast, one-shot execution (no extra console window)
PowerShell -NoExit c:tempscript.ps1

Consult Microsoft’s “PowerShell Performance Tuning” documentation for buffer sizes (default host buffer: 300 lines) and WinRM throttling values (MaxEnvelopeSizeKB, MaxConcurrentCommands). Kernel‑level tuning via netsh interface tcp set global (e.g., autotuninglevel=normal) improves remote script transfer throughput when using Invoke-Command with -SessionOption.

Frequently Asked Questions

What is the difference between `powershell -File script.ps1` and `powershell -Command “& ‘.script.ps1′”`?

Answer: `-File` runs the script in a new session with exit code propagation. powershell.exe -File script.ps1 sets `$LASTEXITCODE` from script’s exit code. `-Command “& ‘.script.ps1′”` does not forward exit codes unless script ends with `exit $LASTEXITCODE`. For reliable automation in CI/CD, use `-File`.

powershell -ExecutionPolicy Bypass -File "C:scriptsdeploy.ps1"

When should I use the `-ExecutionPolicy Bypass` flag when running a script from PowerShell?

Answer: Use `-ExecutionPolicy Bypass` in non-interactive contexts (CI/CD, remote execution) to override restrictive policies without modifying the system. It is a launch flag, not a cmdlet. It runs the script with no restrictions for that session only. Avoid it on locked-down production servers unless signed scripts are unavailable. Prefer `RemoteSigned` when possible.

pwsh -NoProfile -ExecutionPolicy Bypass -File "bootstrap.ps1"

How do I fix error “File cannot be loaded because running scripts is disabled on this system” (PSSecurityException)?

Answer: The error occurs when the execution policy (default on Windows: `Restricted`) blocks script execution. Temporary fix: powershell -ExecutionPolicy Bypass -File script.ps1. Permanent fix: Set-ExecutionPolicy RemoteSigned -Scope CurrentUser. For enterprise environments, use Group Policy (Computer Configuration > Administrative Templates > Windows Components > Windows PowerShell).

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser -Force

Does `powershell -File script.ps1` work on Linux when using PowerShell Core (pwsh)?

Answer: Yes. On Linux, ensure the script uses cross-platform syntax (forward slashes in paths, no Windows-specific modules). Execution policy handling differs: Linux defaults to `Unrestricted`, so `-ExecutionPolicy` is rarely needed. Test with:

pwsh -NoProfile -File ./deploy.ps1

What is the fastest way to run a PowerShell script in a CI/CD pipeline (Azure DevOps, GitHub Actions)?

Answer: Use pwsh -NoProfile -NonInteractive -ExecutionPolicy Bypass -File script.ps1. `-NoProfile` skips loading profile scripts (~200ms savings). `-NonInteractive` suppresses interactive prompts. For Azure DevOps, use the `PowerShell@2` task with `targetType: ‘filePath’`. For GitHub Actions, use `shell: pwsh` with the script path.

pwsh -NoProfile -NonInteractive -ExecutionPolicy Bypass -Command "& { .pipeline.ps1; if ($?) { exit 0 } else { exit 1 } }"