17

I started using Visual Studio Code for Powershell scripting. I'd like to turn off the check for unsigned code, but cannot find out how to do this. I have not found anything in the forums either.

TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87
Ryan Schoenhard
  • 187
  • 1
  • 1
  • 3

7 Answers7

20

you can adjust the policy by adding arguments to the powershell.exe command. for this, open the settings json file. then add the following line:

    "terminal.integrated.shellArgs.windows": "-ExecutionPolicy ByPass",

EDIT The above answer may be deprecated as mentioned in this answer and this VS Code deprecation. See an alternative approach in this answer, or following the example seen in the VS Code post, but adding "args" to set

"terminal.integrated.profiles.windows": {
  "PowerShell": {
    "source": "PowerShell",
    "overrideName": true,
    "icon": "terminal-powershell",
    // "env": {
    //   "TEST_VAR": "value"
    // },
    // -------
    // this part is needed:
    "args": [ "-ExecutionPolicy", "Bypass"]
  }
},
"terminal.integrated.defaultProfile.windows": "PowerShell",
Nate Anderson
  • 18,334
  • 18
  • 100
  • 135
user287107
  • 9,286
  • 1
  • 31
  • 47
  • 1
    This is safest option if you don't need to run scripts outside of vscode – Eugene May 05 '20 at 21:34
  • 1
    Since the question is about using Visual Studio Code for Powershell scripting, I think that this is the best answer. – rpet Jun 01 '20 at 12:05
  • Tip: You need to restart Visual Studio Code after saving the changes. – Blarzek May 24 '21 at 10:45
  • 7
    It appears that this setting is now deprecated in the latest versions of VS Code. – Eric Jun 06 '21 at 04:44
  • This is the best answer as of June 2023 since directly setting execution policy inline only works for the rest of that integrated terminal/VSCode session. – HaulinOats Jun 05 '23 at 20:03
16

I solved by setting the policy as :

Set-ExecutionPolicy –ExecutionPolicy RemoteSigned

to the Visual code integrated environment running as administrator.

The following blog clearly lists the steps to solve it:

http://donovanbrown.com/post/Using-PowerShell-in-VS-Code

d219
  • 2,707
  • 5
  • 31
  • 36
Enzo
  • 156
  • 1
  • 6
14

PowerShell's default settings help prevent malicious scripts from being run. By default you should / can run scripts on the VSCode integrated terminal.

To change PowerShell security settings open PowerShell with admin privileges and run the command:

Get-ExecutionPolicy -List

You should get a response like this:

        Scope ExecutionPolicy
    ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser       RemoteSigned  
 LocalMachine       AllSigned

The CurrentUser is probably Undefined as well since you can't run scripts on your VSCode terminal.

To change that, run this command:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Reopen the VSCode Terminal, it should be working now.

If you want more details, you can find a full docs here: https://learn.microsoft.com/es-es/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7

somebadhat
  • 744
  • 1
  • 5
  • 17
Arias.Dev
  • 151
  • 2
  • 6
2

You would use the command Set-ExecutionPolicy to change the execution policy of your system. You'll need to do so from an administrative session. You can find the appropriate syntax by using the command help Set-ExecutionPolicy from a PowerShell prompt.

You can also find command references online, for example SS64 and Technet.

There's also a highly visible Q&A here on Stack Overflow with the same info.

YetAnotherRandomUser
  • 1,320
  • 3
  • 13
  • 31
  • I tried. here is what I got back:PS C:\Users\admrschoenhard\Desktop> Set-ExecutionPolicy undefined PS C:\Users\admrschoenhard\Desktop> c:\Users\admrschoenhard\Desktop\Untitled-1.ps1 – Ryan Schoenhard Nov 02 '17 at 14:38
  • import-module : File \\us.aegon.com\ait\dsst\Powershell\Modules\Bluecat.psm1 cannot be loaded. The file \\us.aegon.com\ait\dsst\Powershell\Modules\Bluecat.psm1 is not digitally signed. You cannot run this script on the current system. For more information about running scripts and setting execution policy, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170. At C:\Users\admrschoenhard\Desktop\Untitled-1.ps1:1 char:1 + import-module \\us.aegon.com\ait\dsst\Powershell\Modules\Bluecat.psm1 – Ryan Schoenhard Nov 02 '17 at 14:39
  • + CategoryInfo : SecurityError: (:) [Import-Module], PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess,Microsoft.PowerShell.Commands.ImportModuleCommand – Ryan Schoenhard Nov 02 '17 at 14:39
  • Quick note - When I pull out the import-module statement it allows me to set the execution policy to undefined. It's only when trying to import the unsigned module that I get the error that I can't set the policy to unsigned. – Ryan Schoenhard Nov 02 '17 at 14:45
  • It looks like I misunderstood what you were trying to do. Disabling unsigned scripts is something you can do in an administrative interactive shell. I think it would defeat the purpose of the command if you could just put that in any and all scripts. As for Visual Studio Code, what happens when you try to use the PowerShell IDE? Edit your question to include your existing code and the output errors. It's painful here in the comments. Blueshell looks like a professional package, but I was not able to find a page with the psm1 file. What do their direction say about installing it? – YetAnotherRandomUser Nov 03 '17 at 15:06
2

Go to Select default shell

enter image description here

then select Command prompt instead of Windows power shell it will work

enter image description here

harsha kumar Reddy
  • 1,251
  • 1
  • 20
  • 32
0

I have used the below command and worked for me every time in visual studio code You can try this to resolve the PowerShell permission issue

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Manoj Tyagi
  • 169
  • 2
  • 3
0

The suggested commands did not work for me, but the below has fixed it, https://caiomsouza.medium.com/fix-for-powershell-script-not-digitally-signed-69f0ed518715. But it works only for a single session.

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Naod Agere
  • 39
  • 1
  • 6
  • It would probably work for more than a single session if the `-Scope` was something other than `Process`. Maybe `User`? – zzxyz Nov 28 '22 at 21:33