73

It was working fine and then I got an error. After solving it, I still keep getting this error, regardless of whatever project it is.

Output:

& : 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.
    At line:1 char:3
    + & c:/Users/pc/Documents/python/venv/Scripts/Activate.ps1
    +   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : SecurityError: (:) [], PSSecurityException       
        + FullyQualifiedErrorId : UnauthorizedAccessenter code here
Shawn Hemelstrand
  • 2,676
  • 4
  • 17
  • 30
Ahmed Adel
  • 753
  • 1
  • 4
  • 8
  • 2
    possible duplicate of this https://stackoverflow.com/a/49112322/13126651 – Jatin Mehrotra Apr 18 '21 at 15:47
  • thanks i saw it and it solved the problem – Ahmed Adel Apr 18 '21 at 15:52
  • 3
    Does this answer your question? [ps1 cannot be loaded because running scripts is disabled on this system](https://stackoverflow.com/questions/41117421/ps1-cannot-be-loaded-because-running-scripts-is-disabled-on-this-system) – DevThiman Feb 27 '23 at 10:44

12 Answers12

177

This is because the user your running the script as has a undefined ExecutionPolicy You could fix this by running the following in powershell:

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
Malakai
  • 3,011
  • 9
  • 35
  • 49
mcdonalds291
  • 2,024
  • 1
  • 5
  • 14
24

If you are getting error like this,

enter image description here

We can resolve that using the following steps,

  • Get the status of current ExecutionPolicy by the command below:

    Get-ExecutionPolicy
    

    By default it is Restricted. To allow the execution of PowerShell scripts we need to set this ExecutionPolicy either as Unrestricted or Bypass.

  • We can set the policy for Current User as Bypass by using any of the below PowerShell commands:

    Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted -Force
    Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force
    

    Unrestricted policy loads all configuration files and runs all scripts. If you run an unsigned script that was downloaded from the Internet, you are prompted for permission before it runs.

    Whereas in Bypass policy, nothing is blocked and there are no warnings or prompts during script execution. Bypass ExecutionPolicy is more relaxed than Unrestricted.

Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
20

Might also wanna consider setting it to:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

You'll get a message about Execution Policy change (obviously) to which I said "A" for yes to all. Select what works best for you.

This should allow you to run your own scripts but any originating from anywhere else will require approval.

*above post edited for clarity

L. Nader
  • 301
  • 2
  • 3
  • 1
    hi please add the command line for `Remote-Signed` i believe this is a better solution than `Unrestricted` :) – kevinnls Jan 06 '22 at 07:03
10

Just type this in the powershell

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted 

and it will be enabled

5

Just open Windows Powershell as administrator and execute this command Set-ExecutionPolicy Unrestricted -Force. Issue will be resolved and you can activate it in VS code or CMD.

Abdul Qayyum
  • 53
  • 1
  • 4
4
step 1: -Press the windows-button on your keyboard.
step 2: -Type ‘PowerShell’
step 3: -Right-click Windows PowerShell
step 4: -Click Run as Administrator
step 5: -Run the following command and confirm with ‘Y’
Try this. 

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
Kumar Aman
  • 41
  • 3
  • 3
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 08 '22 at 11:30
1

This is what worked for me (Win11, PowerShell as Admin):

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Unblock-File (Join-Path $env:PYENV 'bin/pyenv.ps1')
SteveS
  • 3,789
  • 5
  • 30
  • 64
0

step 1: -Press the windows-button on your keyboard.

step 2: -Type ‘PowerShell’

step 3: -Right-click Windows PowerShell

step 4: -Click Run as Administrator

step 5: -Run the following command and confirm with ‘Y’ Try this.

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
JialeDu
  • 6,021
  • 2
  • 5
  • 24
0

first run:

Get-ExecutionPolicy

then run:

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted -Force
Skandix
  • 1,916
  • 6
  • 27
  • 36
0

Because it is restricted. So type this command in PowerShell Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted

-1

workon "namefolder"

.\venv\scripts\activate

Why Pa
  • 1
  • 3
    PLease give explanantion and details .. os ... langage ... commands .. error message copy ... – Malo Jun 12 '21 at 12:20
-1

As it is written in the documentation of pyenv-win github

If you are getting any UnauthorizedAccess error as below then start Windows PowerShell with the "Run as administrator" option and run Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine, now re-run the above installation command.

X.C.
  • 703
  • 9
  • 18