“.ps1 cannot be loaded because running scripts is disabled on this system” PowerShell Error

Encountering this error? It’s caused by restrictive execution policies that block scripts for security reasons. This guide explains the issue and provides actionable solutions.

PowerShell restricts script execution by default. Running a script like my example cdk.ps1 may trigger the following error message:

Script.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https://go.microsoft.com/fwlink/?LinkID=135170

cannot be loaded because running scripts is disabled on this system

This happens due to PowerShell’s default security settings, which restrict script execution to prevent unauthorized or malicious scripts from running.

Why Does This Happen?

By default, PowerShell applies an Execution Policy that controls how scripts are executed. These policies include:
Restricted: No scripts are allowed to run (default setting).
AllSigned: Only scripts signed by a trusted publisher can run.
RemoteSigned: Local scripts can run, but downloaded scripts need to be signed.
Unrestricted: All scripts can run without restrictions (not recommended).

The Fix: Change Your Execution Policy

To allow PowerShell scripts to run on your system, you need to change the execution policy. Follow these steps:

Step 1: Open PowerShell as Administrator

Step 2: Check Your Current Execution Policy

# Check current execution policy status
Get-ExecutionPolicy -List
Get-ExecutionPolicy -List

Step 3: Modify the Execution Policy

To allow local scripts and signed remote scripts to run, execute the following command:

# Set local execution policy to remote signed
Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy RemoteSigned
Set-ExecutionPolicy PowerShell

Alternatively, if you need a temporary solution that resets after closing PowerShell, run:

# Set local execution policy for current session
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
Set-ExecutionPolicy Local Machine

Step 4: Confirm the Change

Run the following command to verify your new execution policy:

# Check current execution policy status
Get-ExecutionPolicy -List

If the policy is now set to RemoteSigned, you should be able to run your script without issues.

Important Considerations

Security First:
Avoid setting your execution policy to Unrestricted, as it allows all scripts to run, which can be a security risk.

For One-Time Script Execution:
Use Bypass at the process level to avoid permanently changing security settings.

If Running on a Corporate System:
Your execution policy may be managed by an IT administrator. In such cases, contact your IT team for assistance.


Comments

One response to ““.ps1 cannot be loaded because running scripts is disabled on this system” PowerShell Error”

  1. Tom Bosui Avatar

    Thnx PeterI used to do Unrestricted (https://www.itexperience.net/fix-file-cannot-be-loaded-because-running-scripts-is-disabled-on-this-system/), but using RemoteSigned looks like a much more safe solution.
    Is it also possible to run Remote powershell scripts, that are NOT signed, by copy-pasting the source code into my own ps1 file?

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts
Categories
Tags

Always On Availability Groups (AAG) (4) AWS (4) AWS Redshift (6) Certificates & Encryption (3) Change Data Capture (CDC) (2) Database Admin (72) Database Backups & Recovery (14) Database Mirroring (2) Deleting Data (1) Error Messages (5) Git Commands (6) Importing & Exporting Data (2) Linked Servers (3) Linux Admin (2) Logging & Monitoring (1) Measuring Databases (10) Microsoft Patching (2) MySQL (4) Postgres (6) PowerShell Scripts (1) SQL Server Agent (5) SQL Server Database Files (1) SQL Server Data Types (2) SQL Server Management Studio (SSMS) (15) SQL Server Network Connectivity (3) SQL Server on Linux (1) SQL Server Patching (2) SQL Server Performance (6) SQL Server Permissions (2) SQL Server Processes (SPIDs) (7) SQL Server Replication (2) SQL Server Scripts (13) Windows Admin (21) Windows Authentication (2) Windows Automation (1) Windows Events (2) Windows Firewall (4) Windows Subsystem for Linux (WSL) (18)