Visual studio code cmd error: Cannot be loaded because running scripts is disabled on this system

190,268

Solution 1

I found out here that you can add to your visual studio code settings the following and the problem will vanish: For visual studio code settings, go to File -> Preferences -> Settings -> Extensions -> Scroll down and find "Edit in settings.json". Do not forget to restart the visual studio code

"terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"]

The reason is that, in build command line integrations like visual studio code, you need to set the command line policies by your self. By setting the above configurations, the visual studio code will do that for you.

(read this to understand better the command line policies)

Update August 2021

It seems that terminal.integrated.shellArgs is deprecated. This answer shows how to do it in new versions.

Solution 2

the issue is that PowerShell by default restricts running scripts unless we change the execution policies.

we can change the execution policies by adding -ExecutionPolicy Bypass args. in vscode you will have to create a shell profile with these args by adding below in settings.json (ctrl + shift + p and type "settings.json")

"terminal.integrated.profiles.windows": {
  "PowerShell": {
    "source": "PowerShell",
    "icon": "terminal-powershell",
    "args": ["-ExecutionPolicy", "Bypass"]
  }
},
"terminal.integrated.defaultProfile.windows": "PowerShell",

Need to restart VS Code and the Terminal.

Note: terminal.integrated.shellArgs settings have been deprecated.

Solution 3

For more simplicity, I want to add the vs code settings path in addition to Ricardo's answer. you can get it like this:

File -> Preferences -> Settings and in the search bar write "automation".

After that, by looking your operating system enter "edit in settings.json".

Finally, add the following b/n the braces:

"terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"]

enter image description here

Solution 4

The simplest way to deal with this problem from Visual Studio Code is to change from powerShell to cmd (assuming there's no reason for using powershell in your case). To do so, follow these few steps:

  1. Terminal -> New Terminal
  2. In right lower corner select "Select Default Shell" option

enter image description here

  1. Select "Command prompt"

enter image description here

I have not found such solution among current answers. Hope that helps!

Solution 5

Face the same issue and this works for me. open PowerShell as Administrator and paste this.

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser  
Share:
190,268
Ricardo Rocha
Author by

Ricardo Rocha

🧐 Who is this man? 💻 Full Stack Web Developer and in love with javascript and everything around. 👨‍💻 Works daily with C#, angular, and SQL and likes it! ✨ Deep learning enthusiastic, especially if works with javascript 😀 📚 Learn addicted. 🤓 Proud nerd! 🎷 Saxophone player.

Updated on February 02, 2022

Comments

  • Ricardo Rocha
    Ricardo Rocha about 2 years

    Inside of visual studio code, I'm trying to execute a script.bat from the command line, but I'm getting the following error:

    File C:\Theses_Repo\train-cnn\environment\Scripts\activate.ps1 cannot be loaded because running scripts is disabled on this system.

    After reading this I tried to run the visual studio code in administrator mode, thinking that the problem was a matter of privileges. But the error is throwing anyway.

  • Jacob Fortuna
    Jacob Fortuna over 4 years
    This fixed the issue for one run, but then it came back. The solution above worked for me.
  • Friedrich Siever
    Friedrich Siever over 4 years
    Thanks a million. This did the trick for me (after 1h of research) and saved me another headache.
  • Aros
    Aros about 4 years
    OK, my mistake. I also had to set Powershell as my default terminal.
  • Sorin Veștemean
    Sorin Veștemean about 4 years
    Thank you sir! After you add this in settings.json , you should reload or reopen vscode, a notification will ask you if you want to grant access for execution policy.
  • Syafiqur__
    Syafiqur__ about 4 years
    This did me. Thanks for the guide
  • Andy
    Andy almost 4 years
    Where in the VSCode settings do you add this?
  • Ricardo Rocha
    Ricardo Rocha almost 4 years
    @Andy You have in the link associated with the phrase "visual studio code settings "
  • CCC
    CCC over 3 years
    @Andy, Go to File -> Preferences -> Settings -> Extensions -> Scroll down and find "Edit in settings.json"
  • ORCAs
    ORCAs over 3 years
    thank you very much. Now i got more time to core developping.
  • LondonAppDev
    LondonAppDev over 3 years
    This might work but sounds like bad practice from a security standpoint.
  • Mohammed Noureldin
    Mohammed Noureldin over 3 years
    For me I searched for terminal.integrated.shellArgs as @Barani mentioned.
  • Corina Roca
    Corina Roca over 3 years
    Go to the @null comment (three posts below) and you can see that you just have to change from Powershell to CMD as default command prompt. That's all!
  • ajpieri
    ajpieri about 3 years
    Or just hit ctrl + shift + p and type "Open settings.json". You will see the option. Just click it and it will open your settings.json
  • Vincenzooo
    Vincenzooo about 3 years
    there are one million of "Edit in settings.json". Javed's answer worked for me.
  • MoishyS
    MoishyS almost 3 years
    Note: defaultProfile is known not to work on the initial restored terminal #123188 - when you open the terminal from the + button it should work. hopefully will get fixed soon.
  • Tigger
    Tigger almost 3 years
    As of May 2021 this is the correct solution. The old method of setting terminal.integrated.shellArgs.windows no longer works.
  • veritaS
    veritaS almost 3 years
    Just some more additional information on this
  • Green Y.
    Green Y. almost 3 years
    Thank you. And it needs to restart the VS code.
  • AbuDawood
    AbuDawood over 2 years
    the shellArgs is deprecated in the new versions as it answered below stackoverflow.com/a/67420296/2514158 @MoishyS
  • lov2code
    lov2code over 2 years
    This worked perfectly. The previous answers are depreciated in VS Code. Thank you!
  • user2059013
    user2059013 over 2 years
    After entering the above command it works
  • PG_eon
    PG_eon over 2 years
    Had to restart the powershell within vscode to make this work. Thanks.
  • user2552108
    user2552108 over 2 years
    This works, just needed to restart VS Code and the Terminal afterwards
  • Punit
    Punit over 2 years
    why do we have shit tools like vscode , just use pycharm , its way better
  • Priyank Kotiyal
    Priyank Kotiyal over 2 years
    I think people working on windows finds it better, WebStorm is also good but its paid.
  • DoomGoober
    DoomGoober over 2 years
    VSCode might give you three options of settings.json: workspace settings, default settings, and just settings. It didn't work for me unless I chose the last option: Open Workspace Settings.
  • Chris Macaluso
    Chris Macaluso about 2 years
    Thank God this worked, thank you
  • Punit
    Punit about 2 years
    i use windows , i generally use both of them (to keep me updated) , my GitHub desktop is connected to vscode for final reviews/comments but my development env. is pycharm , and pycharm is so much better.
  • Ani
    Ani about 2 years
    works perfectly. I also had to start a new powershell inside VSCODe with + button.
  • Nam Lê Quý
    Nam Lê Quý almost 2 years
    thank you so much. I was very upset after installing typescript and got this error
  • Jovane Mullings
    Jovane Mullings almost 2 years
    where should you put this?
  • Cyebukayire
    Cyebukayire almost 2 years
    Hi @JovaneMullings I've edited my answer with clear path of where you need to paste this. Thank you:)
  • sskular
    sskular almost 2 years
    Might be a silly question but what's b/n here?
  • user1011627
    user1011627 almost 2 years
    This solution kind of worked for me. I was unable to change the settings.json file as it was read-only when doing it this way. I had to combine with a "flavor" of Abdu's answer. I had to use File->Preferences->Settings and select "Edit in settings.json" in order to modify the file. I searched for "profiles" instead of "automation" though....not sure what the automation settings are, but they didn't work for me.