PowerShell .ps1 file on Visual Studio post build event

tugberk picture tugberk · Sep 2, 2011 · Viewed 11.5k times · Source

I am trying to execute the following post build event code but I am getting an non-useful error :

"c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -file "$(SolutionDir)tools\nuget_pack.ps1"

I have run the following PS script before I try :

Set-ExecutionPolicy unrestricted

What am I missing?

UPDATE

This is strange, I am not getting an error on VS now. but the script is not working. When I run it with powershell console I get the following error :

enter image description here

Answer

Keith Hill picture Keith Hill · Sep 3, 2011

Visual Studio writes the post-build event script to a .BAT file and executes that using cmd.exe. So using & "<path-to-powershell>" won't work. Just execute:

Powershell.exe -file "$(SolutionDir)tools\nuget_pack.ps1"

And if you think you're likely to run into execution policy issues on other machines that build the solution, consider going this route:

Powershell.exe -ExecutionPolicy Unrestricted -file "$(SolutionDir)tools\nuget_pack.ps1"