Solution to your problem :
For some reason, account on which you run VS on and on which VS hosts PowerShell has restricted ExecutionPolicy (you can type Get-ExecutionPolicy , select the text and hit CTRL+F8 and should see Restricted as the answer).
So the solution is simple (should be): override the settings and set it to RemoteSigned with this command :
select the text, hit CTRL+F8. Now hitting F5 should make your PS behave.
(Alternatively Unrestricted is also viable option, but this is considered unsafe. More on this here : http://technet.microsoft.com/en-us/library/ee176961.aspx)
More on the topic :
I wanted to create some PowerShell script and I thought there has to be something better than PowerShell ISE (sry, I am just too used to VS and debugger and all that goodness I have at my disposal) so I discovered this wonderful plugin for VS 2012 – PowerShell Tools for Visual Studio – http://visualstudiogallery.msdn.microsoft.com/c9eb3ba8-0c59-4944-9a62-6eee37294597 and all worked as expected, until (as far as I remember) I restarted OS and had this strange error in VS in my output window :
Error: File C:\users\your_name\documents\visual studio 2012\Projects\PowerShell Script Project1\PowerShell Script Project1\Script.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.
The program ‘PowerShell Script’ has exited with code 0 (0x0)
(Small side note here to make you more puzzled but this might also help you in some way : I work in a company where admins takes security seriously and our “normal” day to day accounts are restricted and don’t have access to this and that so we have another accounts to install sw, make changes to the system, etc. I need to run my VS under this account, not my own.)
Great, no what? There is a link to documentation about what execution policies are and how to set them, etc, I changed it from vanilla restricted state to RemoteSigned and the very same .ps1 file worked after I set RemoteSigned for my and my admin account. It looks like PS stores policies per user in registry. More on this here : http://dusan.kuzmanovic.net/2012/01/04/powershell-execution-policy/
Than I had “brilliant” idea to try PowerShell commands in NuGet Package Manager Console because it is using PowerShell under the hood, I was just looking for some prompt to query the ExecutionPolicy. And the magic happened, after query the PowerShell worked!!!! Brilliant but this is just workaround and not the solution.
After this the best idea seemed to contact the author so I mailed directly Adam Driscoll (apologies for distraction) which led to few discoveries like :
- Writing some text in the .ps1 file in VS and selecting some code and right click reveals Execute selection option that is able to run your code event if it fails running it F5,
- Adam downloaded in his spare time NuGet sources and it looks like : “NuGet sets the process scoped execution policy to RemoteSigned when it is initialized. “
So next logical step would be to try to run Set-ExecutionPolicy from VS to set it for account that VS is using to run PowerShell. And it looks like it is working.
Enjoy guys and thanks Adam for your plugin and your time.