Skip to content

Fixing PowerShell Execution Policy Error for Scripts

Problem Overview

When trying to run PowerShell scripts (particularly Python virtual environment activation scripts), you may encounter this security error:

File C:\Users\pc\Documents\python\venv\Scripts\Activate.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.

This occurs because Windows PowerShell has a security feature called Execution Policy that restricts script execution by default to prevent potentially harmful scripts from running unintentionally.

Understanding Execution Policies

PowerShell uses several execution policy levels to control script execution security:

  • Restricted: No scripts allowed (Windows default)
  • AllSigned: Only signed scripts from trusted publishers
  • RemoteSigned: Local scripts allowed, remote scripts require signing
  • Unrestricted: All scripts allowed with warnings for remote scripts
  • Bypass: No restrictions or warnings

Check Current Execution Policy

First, determine your current execution policy:

powershell
Get-ExecutionPolicy

For most users, setting the execution policy for the current user is the safest approach:

powershell
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned

RemoteSigned allows you to run local scripts while maintaining security for downloaded scripts.

TIP

Type A when prompted to confirm the policy change for "All."

Solution 2: More Permissive Approach (Development Only)

If you frequently work with scripts in development environments:

powershell
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted

WARNING

This approach will prompt you before running unsigned scripts downloaded from the internet. Use with caution.

Solution 3: Administrator Approach (System-Wide)

For system-wide changes, run PowerShell as administrator:

powershell
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine

DANGER

Run this only if you understand the system-wide implications. Not recommended for shared computers.

Solution 4: Bypass Policy (Temporary)

For one-time script execution without changing policy:

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

Detailed Command Breakdown

Set-ExecutionPolicy Parameters

  • -Scope: Defines which user(s) the policy affects

    • CurrentUser: Affects only your account (safest)
    • LocalMachine: Affects all users on the computer
    • Process: Temporary policy for current session
    • UserPolicy: Group Policy setting for users
  • -ExecutionPolicy: Security level

    • RemoteSigned: Recommended balance of security and functionality
    • Unrestricted: Less secure but fewer prompts
    • Bypass: No security restrictions (use cautiously)
  • -Force: Suppresses confirmation prompts

    powershell
    Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned -Force

PowerShell Execution Policy Comparison

PolicyLocal ScriptsRemote ScriptsSecurity Level
Restricted❌ Blocked❌ BlockedHighest
AllSigned✅ If signed✅ If signedHigh
RemoteSigned✅ Allowed✅ If signedRecommended
Unrestricted✅ Allowed⚠️ With warningLow
Bypass✅ Allowed✅ AllowedNone

Best Practices

  1. Use CurrentUser scope when possible to avoid affecting other users
  2. Prefer RemoteSigned over Unrestricted for better security
  3. Run as Administrator only when absolutely necessary
  4. Consider temporary bypass for one-time script execution
  5. Check execution policy before troubleshooting script issues

Common Scenarios

Python Virtual Environments

For Python development, you'll typically need to allow execution of Activate.ps1 scripts:

powershell
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned

Project-Specific Solutions

For pyenv-win specifically, you may also need to unblock the script file:

powershell
Unblock-File (Join-Path $env:PYENV 'bin/pyenv.ps1')

Troubleshooting

If you still encounter issues after changing execution policy:

  1. Restart PowerShell after changing execution policy
  2. Verify the change took effect with Get-ExecutionPolicy -List
  3. Check Group Policies if policy changes don't persist
  4. Run PowerShell as Administrator if you lack sufficient privileges

INFO

Execution policies are not security boundaries - they're designed to prevent accidental script execution, not malicious bypass attempts. For true security, use other Windows security features alongside execution policies.

By understanding and properly configuring PowerShell execution policies, you can maintain security while enabling the script execution necessary for development work.