ProfK
ProfK

Reputation: 51114

Can't bypass `Restricted` execution policy

I a tool just downloaded that opens a Visual Studio Command prompt from within the IDE, in the root of the current project. My main gripe is that it opens an old style command window, where I would rather have a PowerShell window. According to this post, this simple change should allow this:

cmd.exe /k ""%VS120COMNTOOLS%VsDevCmd.bat" & powershell"

When I this command from outside of VS 2015, it seems to work fine and gives me a PowerShell window. Yet when I try and run it from inside VS, using the utility's menu item, it gives me this error:

Cannot load PSReadline module. Console is running without PSReadline. . : File C:\Users\brady\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170. At line:1 char:3 + . 'C:\Users\brady\Documents\WindowsPowerShell\Microsoft.PowerShell_pr ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : SecurityError: (:) [], PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess PS C:\Development\vNext\Commerce\src\Commerce.Test> Get-Execution-Policy PS C:\Development\vNext\Commerce\src\Commerce.Test> Get-ExecutionPolicy Restricted

My global execution policy is RemoteSigned, but in the same window that show me the error, when I run a Get-ExecutionPolicy, the returned value is Restricted.

I have tried modifying my command to include the PS switch:

cmd /k ""%VS140COMNTOOLS%VsDevCmd.bat" & powershell -ExecutionPolicy Bypass" 

But this still gives me exactly the same error.

The output of the command suggested by @PetSerAl in the comments

[Environment]::Is64BitOperatingSystem;[Environment]::Is64BitProcess;Get-Executi‌​onPolicy -List

gives two different results. The first in a normal PS window external to VS:

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

And the second in the only PS window I can find inside VS, the Package Manager:

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

Upvotes: 1

Views: 1780

Answers (1)

Kory Gill
Kory Gill

Reputation: 7161

You can configure an external command and optionally configure a keystroke for it.

I just did this in my VS2015...neat!

Create a cmd file

Create cmd file to call VsDevCmd.bat (Developer Command Prompt for VS2015) and then PowerShell.

dev14powershell.cmd

call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat"
powershell -ExecutionPolicy Bypass

Configure External Tool

Tools -> External Tools

VS2015 External Tools

Calls the cmd file above, starts in Solution Dir (configurable)

Run the new External Tool

Run PowerShell External Tool

Result

A new command window started in Solution Directory.

Optional, configure keyboard shortcut

Tools -> Options -> Keyboard

Search for external and remember the number/order of the command you created (4 in my case)

Command shortcut key

Hit assign and you have:

Powershell Shortcut

Upvotes: 1

Related Questions