PowerShell .ps1 file on Visual Studio post build event PowerShell .ps1 file on Visual Studio post build event powershell powershell

PowerShell .ps1 file on Visual Studio post build event


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" 


You can reproduce the error in Powershell as follows:

"this is a string" -file "my.ps1"

It is taking the first as a string, the -file as the -f format flag and saying it doesn't have a value expression on the right for the format substitution.

Try like this:

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

(as Keith notes, this will not work as this is run from a bat file than Powershell.)

Or just:

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


Before calling power-shell script from visual studio, set the ExecutionPolicy to unrestricted from power-shell window like this...

Set-ExecutionPolicy -Scope CurrentUser;ExecutionPolicy: unrestricted;

the call power-shell script in the following manner...

powershell.exe $(SolutionDir)Setup.ps1 -SolutionDir $(SolutionDir) -ProjectPath $(ProjectPath)

enter image description here

then in the script, you can always read the parameter like this...

param([string]$SolutionDir,     [string]$ProjectPath);#Write-Host ($SolutionDir +" Call this script with following aruments");#Write-Host ($ProjectPath +" Call this script with following aruments");