224

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.

Ricardo Rocha
  • 14,612
  • 20
  • 74
  • 130
  • 9
    1. Press the windows-button on your keyboard. 2. Type ‘PowerShell’ 3. Right-click Windows PowerShell 4. Click Run as Administrator 5. Run the following command and confirm with ‘Y’ Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine Referred to link https://www.roelpeters.be/solved-running-scripts-is-disabled-on-this-system-in-powershell/ – S Anil Kumar Dora May 13 '21 at 10:35

28 Answers28

313

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.

Ricardo Rocha
  • 14,612
  • 20
  • 74
  • 130
  • 2
    Thanks a million. This did the trick for me (after 1h of research) and saved me another headache. – Friedrich Siever Dec 08 '19 at 13:19
  • OK, my mistake. I also had to set Powershell as my default terminal. – Aros Mar 10 '20 at 17:28
  • 3
    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. – Sorin Veștemean Mar 12 '20 at 06:35
  • 3
    Where in the VSCode settings do you add this? – Andy Jun 08 '20 at 09:20
  • 1
    @Andy You have in the link associated with the phrase "visual studio code settings " – Ricardo Rocha Jun 08 '20 at 09:29
  • 1
    @Andy, Go to File -> Preferences -> Settings -> Extensions -> Scroll down and find "Edit in settings.json" – CCC Aug 26 '20 at 21:08
  • thank you very much. Now i got more time to core developping. – ORCAs Oct 04 '20 at 12:58
  • For me I searched for `terminal.integrated.shellArgs` as @Barani mentioned. – Mohammed Noureldin Jan 04 '21 at 03:47
  • 1
    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! – Corina Roca Jan 10 '21 at 11:39
  • 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 – ajpieri Mar 28 '21 at 03:57
  • there are one million of "Edit in settings.json". Javed's answer worked for me. – Vincenzooo Apr 13 '21 at 21:36
  • 1
    the shellArgs is deprecated in the new versions as it answered below https://stackoverflow.com/a/67420296/2514158 @MoishyS – AbuDawood Aug 22 '21 at 10:41
  • Why suggest the "Bypass" policy by default? Wouldn't "RemoteSigned" be safer? – Marcos Pereira Jan 26 '23 at 18:10
  • Great. This answer helped me to get rid of this exact problem occurred to me. Just to describe the steps little more.. After you go to the **File -> Preferences -> Settings -> Extensions -> Scroll down and find "Edit in settings.json" path**, It will open up **settings.json file**. and there, I added this code like this `{ "terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"], "workbench.colorTheme": "Default Dark+", "json.schemas": [ ] }` – Ish Apr 27 '23 at 14:55
  • Related answer here https://stackoverflow.com/questions/47023796/visual-studio-code-unsigned-powershell-scripts – Nate Anderson May 01 '23 at 18:45
217

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.

MoishyS
  • 2,334
  • 1
  • 6
  • 8
  • 27
    Note: defaultProfile is known not to work on the initial restored terminal [#123188](https://github.com/microsoft/vscode/issues/123188) - when you open the terminal from the + button it should work. hopefully will get fixed soon. – MoishyS May 10 '21 at 18:44
  • 4
    As of May 2021 this is the correct solution. The old method of setting terminal.integrated.shellArgs.windows no longer works. – Tigger May 10 '21 at 22:57
  • 2
    This worked perfectly. The previous answers are depreciated in VS Code. Thank you! – lov2code Aug 27 '21 at 19:18
  • 5
    Had to restart the powershell within vscode to make this work. Thanks. – PG_eon Nov 12 '21 at 18:10
  • This works, just needed to restart VS Code and the Terminal afterwards – user2552108 Jan 18 '22 at 01:58
  • 4
    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. – DoomGoober Jan 23 '22 at 04:15
  • 2
    works perfectly. I also had to start a new powershell inside VSCODe with + button. – Ani Mar 19 '22 at 22:49
  • thank you so much. I was very upset after installing typescript and got this error – Nam Lee May 11 '22 at 16:00
  • 2
    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. – user1011627 Jun 22 '22 at 12:13
  • This solution works: I edited the "User" settings.json (not default, not workspace). Restarting VS Code only didn't pick up the changes. I did what @Ani said and deleted my PowerShell terminal window (use the "Kill terminal" trashcan icon on the right side of the terminal area) then "+" create a new instance. Then my settings were picked up and tsc command worked. – kenswdev Aug 19 '22 at 14:39
151

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.

starball
  • 20,030
  • 7
  • 43
  • 238
hpopiolkiewicz
  • 3,281
  • 4
  • 24
  • 36
108

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

javed
  • 406
  • 3
  • 10
Abdulwehab
  • 1,356
  • 2
  • 10
  • 12
63

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

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser  
  • More info here: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.3&viewFallbackFrom=powershell-6 – Marcos Pereira Jan 26 '23 at 18:07
47
  1. Press the windows-button on your keyboard

  2. Type 'PowerShell'

  3. Right-click Windows PowerShell

  4. Click Run as Administrator

  5. Run the following command and confirm with ‘Y’

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine

for more information: https://www.roelpeters.be/solved-running-scripts-is-disabled-on-this-system-in-powershell/

Hope this would resolve the issue

pix data
  • 731
  • 7
  • 14
23
  1. Open VS code

  2. File -> Prefrences -> settings

  3. in the Search bar -> Search for "terminal.integrated.shellArgs"

  4. Click on the "Edit in setting.json" for Terminal>integrated>ShellArgs > windows [whatever you OS, select the respective settings]

  5. At the top of the json file add this :

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

    For example after adding your json might look like:

{
    "terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"],
    "git.autofetch": true,
    "peacock.favoriteColors": [
        {
            "name": "Angular Red",
            "value": "#b52e31"
        },
        {
            "name": "Auth0 Orange",
            "value": "#eb5424"
        },
  1. restart the VS code and try the command again (in my case i was trying ng serve)

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

Safin Ghoghabori
  • 516
  • 1
  • 6
  • 17
Barani r
  • 2,119
  • 1
  • 25
  • 24
19

Run the below command then the issue will be resolved.

set-ExecutionPolicy RemoteSigned -Scope CurrentUser ( I tried only in windows )

Viplav Soni
  • 1,489
  • 13
  • 18
  • Just some more additional information on [this](https://learn.microsoft.com/en-gb/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.1) – veritaS May 22 '21 at 14:37
15

I have faced the same problem due to security reasons on Windows 10. I managed to fix it by running the following command in PowerShell:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted
Ed The ''Pro''
  • 875
  • 10
  • 22
soumen saha
  • 151
  • 1
  • 3
12
  1. Open VS code
  2. Go to File -> Prefrences -> settings
  3. in the Search bar -> Search for "terminal.integrated.defaultprofile.windows"
  4. Click on the "Edit in setting.json" for Terminal > integrated > Default Profile: Windows [whatever you OS, select the respective settings].

At the top of the JSON file add this :

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

Note: The following settings line is deprecated:

"terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"]
Ahmed AlZabidi
  • 158
  • 1
  • 8
10

I opened VS Code as Administrator and ran this command in the terminal:

Set-ExecutionPolicy Unrestricted

It allowed me to run the scripts without an errors.

Ed The ''Pro''
  • 875
  • 10
  • 22
user1838609
  • 111
  • 1
  • 6
6

For security reasons, in case you share the computer with other users, use: PowerShell:

Set-ExecutionPolicy -Scope CurrentUser Unrestricted

No need for Admin privileges.

Daniel
  • 61
  • 1
  • 1
4

Steps to resolve the issue,

  1. Open PowerShell in your Computer (This will change the PowerShell execution policy on your Windows Computer)
  2. Run ( To get-ExecutionPolicy for your Machine )

Get-ExecutionPolicy -List

  1. Run

Set-ExecutionPolicy -Scope CurrentUser Unrestricted

Paboda Jay
  • 69
  • 4
3

If you don't need to explicitly run this on PowerShell, then you may want to try running it in the command prompt.

Just type cmd and press enter. Doing so will open the command prompt in the current terminal window. It will behave same as a normal command prompt.

bula
  • 8,719
  • 5
  • 27
  • 44
3

For Win10 users

  1. In Visual Studio Code: File -> Preferences -> Settings
  2. Type in the search: terminal.integrated.defaultProfile.windows
  3. Click on the Edit in setting.json
  4. Add this:
"terminal.integrated.profiles.windows": {
  "PowerShell": {
    "path" : [
      "${env:windir}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
    ],
    "source": "PowerShell",
    "icon": "terminal-powershell",
    "args": [
      "-NoLogo",
      "-ExecutionPolicy",
      "ByPass"]
  }
},
"terminal.integrated.defaultProfile.windows": "PowerShell",
  1. Save and restart Visual Studio Code
cizario
  • 3,995
  • 3
  • 13
  • 27
3

The location of tag is changed, Steps:

  1. Do Ctrl+Shift+P and type 'settings' and then choose 'Preferences: Open settings (JSON)': Locating settings

  2. Then add "args": ["-ExecutionPolicy", "Bypass"] inside "terminal.integrated.profiles.windows"."PowerShell" in settings.json : SettingsJSON

  3. Restart VS code.

There can be different steps if anything changed with VS code updates in future.

Priyank Kotiyal
  • 241
  • 2
  • 13
  • why do we have shit tools like vscode , just use pycharm , its way better – dev Jan 19 '22 at 20:55
  • I think people working on windows finds it better, WebStorm is also good but its paid. – Priyank Kotiyal Jan 20 '22 at 06:24
  • 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. – dev Mar 13 '22 at 23:11
2

Added this line in settings.json

"terminal.integrated.defaultProfile.windows": "PowerShell"

For me used following command in VS powershell and it starts working

Set-ExecutionPolicy -ExecutionPolicy Unrestricted
lejlun
  • 4,140
  • 2
  • 15
  • 31
shabnam
  • 61
  • 1
  • 9
1

Run VS-Code as ADMIN.

This fixed my issue on my home PC with running ng new my-App which had produced this error when I first opened VS-Code from the installation process.

1

I had the same problem a few minutes ago: I did this and it worked for me

You need to edit your visual studio settings. Go to File -> Preferences -> Settings -> Extensions -> Scroll down and find "Edit in settings.json". Past these codes below, save, and restart the visual studio code.

   {
    "terminal.integrated.profiles.windows": {
        "PowerShell": {
            "source": "PowerShell",
            "icon": "terminal-powershell",
            "args": ["-ExecutionPolicy", "Bypass"]
        }
    },
    
    "terminal.integrated.defaultProfile.windows": "PowerShell",
    "files.associations": {
        "*.html": "html"
    },
}
Cyebukayire
  • 795
  • 7
  • 14
1

Adding this error message just to find this topic easily in the future:

    ng : File C:\Users\[USER_NAME]\AppData\Roaming\npm\ng.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:1
    + ng help
    + ~~
        + CategoryInfo          : SecurityError: (:) [], PSSecurityException
        + FullyQualifiedErrorId : UnauthorizedAccess

PedroPK
  • 519
  • 6
  • 8
1

Unless you actually require the default terminal be windows Powershell, I would suggest simply changing the type of terminal. It's very easy to do: On the right top part of the shell click on the small down-facing arrow next to the plus sign. Then choose which terminal you would like to use (I like good old CMD) and you can even pick a default for the next time you open a new treminal. enter image description here

Amos Bordowitz
  • 474
  • 6
  • 19
0

This is not a VSCode specific issue, it is a OS one. There are different levels for machine, user, process that allow PowerShell Scripts to run and it must be enabled.

If this is your machine just change the execution policy to RemoteSigned or unrestricted so that is applies to your entire system. If you just want to use it in VSCode, the change your settings file or change you shortcut to include the execution policy you want to use. The Recommendation is RemoteSigned. Meaning all local code will run, but any remote scripts must be signed.

If this is a corporate machine, that has the Execution set to Restricted, then no code will run until that setting is changed. You can still run code by selecting it all in the VSCode editor and hitting F8. Now, if they have enforced this policy, then you need to talk to the IT time to get them to change it for you.

All of this is detailed in the PowerShell Help files.

Get-Command -Name '*executionpolicy*' | ft -AutoSize

CommandType Name                Version Source                       
----------- ----                ------- ------                       
Cmdlet      Get-ExecutionPolicy 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet      Set-ExecutionPolicy 3.0.0.0 Microsoft.PowerShell.Security


# get function / cmdlet details
(Get-Command -Name Get-ExecutionPolicy).Parameters
Get-help -Name Get-ExecutionPolicy -Full
Get-help -Name Get-ExecutionPolicy -Online
Get-help -Name Get-ExecutionPolicy -Examples

    Get-ExecutionPolicy
    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
    Get-ExecutionPolicy -List

(Get-Command -Name Set-ExecutionPolicy).Parameters
Set-help -Name Set-ExecutionPolicy -Full
Set-help -Name Set-ExecutionPolicy -Online
Set-help -Name Set-ExecutionPolicy -Examples

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
    Set-ExecutionPolicy -ExecutionPolicy Restricted
    Invoke-Command -ComputerName "Server01" -ScriptBlock {Get-ExecutionPolicy} | Set-ExecutionPolicy -Force
    Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy AllSigned -Force
    Get-ExecutionPolicy -List
    Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Undefined
    Set-ExecutionPolicy -Scope Process -ExecutionPolicy AllSigned
    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

Note, if your org is controlling this, the again, talk to them before trying to bypass the policy, because if they did this, this also means they are monitoring it, and if you do this that will know. I have seen things like this engender RPE's. (Resume Producing Events)

postanote
  • 15,138
  • 2
  • 14
  • 25
0

I Have this error in my visual studio 2019 (Version 16.8.4). Finally I found solution which is: EnableScripts (Make from PowerShell os windows (from Registry Editor of windows 10)

Brows the PowerShell as bellow and make 0 => 1 and that is it.

Registry Editor

Registry Editor

enter image description here

Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35
kargarf
  • 51
  • 4
0

After tearing my hair out this is what I ended up doing. Instead of using CMD or PowerShell I switched over to BASH. In the settings.json:

//"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\cmd.exe",
// "terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",   
        "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",

The CMD and Powershell here are commented out in case you want to go back to one of those. To get to settings.json:

> File -> Preferences -> Settings -> in search type : "settings.json" ->
> Under [JSON] click "Edit in settings.json"
Brad123
  • 877
  • 10
  • 10
0

I faced a similar issue while trying to use pnpm. I just changed to GitBash instead of PowerShell for vs-code terminal, and it worked.

Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122
0

I'm not clear where should I add it ("terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"]).

I have a code like this in the settings.json file

{
"python.defaultInterpreterPath": "C:\\Users\\Hansamal\\AppData\\Local\\Programs\\Python\\Python36\\python.exe",
"workbench.editorAssociations": {
    "*.ipynb": "jupyter-notebook"
},
"notebook.cellToolbarLocation": {
    "default": "right",
    "jupyter-notebook": "left"
},
"json.schemas": [

]

}

Hansamal
  • 39
  • 4
0

Update Powershell Execution Policy.

Setting a global unrestricted execution policy will run any script. But also shows a warning from windows.

Set-ExecutionPolicy -ExecutionPolicy Unrestricted

Instead allow the script that is being run. You can find the full path of the script from the error.

powershell -ExecutionPolicy Bypass -File C:\Users\Me\AppData\Roaming\npm\webpack.ps1

Here I have provided access to webpack for running.

Sorter
  • 9,704
  • 6
  • 64
  • 74
-1

since we are in 2022, and vdcode has changed the way to fix this issue in setting.json just open the terminal and run

Set-ExecutionPolicy -Scope CurrentUser Unrestricted
JERVI
  • 37
  • 5