Post build event execute PowerShell Post build event execute PowerShell powershell powershell

Post build event execute PowerShell


Here is an example :

First of all : you must be aware of the fact that PowerShell must be configure to execute scripts. The following line allow PowerShell to execute scripts :

Set-ExecutionPolicy RemoteSigned

Special mention here : if you are running a 64bits system you've got to take care of the fact that 'devenv.exe' the Visual Studio 2010 executable is a 32Bits exe, so you need to allow PowerShell 32 to execute scripts.

Once here you can go in your project properties and configure post build as shown here under (sorry in french) :

Post build in VS 2010

For example :

Example of postbuild with powershell

Here is the file 'psbuild.ps1', it creates a 'test.txt' in the target path with the configuration name inside. I put in comment different ways to debug your postbuild script (message box, sound, message on the output)

param ([string]$config, [string]$target)#[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")#[void][System.Windows.Forms.MessageBox]::Show("It works.")#[Console]::Beep(600, 800)#Write-Host 'coucou'set-content $target -Value $config -Force


command Set-ExecutePolicy will temporarily set execution policy under current session. If you set this in powershell and run post build command in vs you will still get not allowed. So set first then run your ps1 script like bellow

powershell -ExecutionPolicy Unrestricted $(ProjectDir)Deploy.ps1 -ProjectDir $(ProjectDir) -TargetPath $(TargetPath)


Instead of messing with system-wide settings and having to differentiate between 32 and 64-bit environments, a much easier and more reliable approach is to specify the ExecutionPolicy in the call to PowerShell, as follows:

C:\Users\xyz>PowerShell -ExecutionPolicy UnrestrictedPS C:\Users\xyz> Get-ExecutionPolicyUnrestrictedPS C:\Users\xyz> exitC:\Users\xyz>PowerShell -ExecutionPolicy RemoteSignedPS C:\Users\xyz> Get-ExecutionPolicyRemoteSigned

Note in the above code how calling Get-ExecutionPolicy tells you the current mode. Also note how this mode is specified in the call to PowerShell itself, which can be combined with a script filename:

test.ps1 contents:

echo ('The current policy is ' + (Get-ExecutionPolicy)).ToString()

Calling test.ps1 with Unrestricted policy on a system having scripts disabled:

C:\Users\xyz>PowerShell -ExecutionPolicy Unrestricted -file test.ps1The current policy is Unrestricted

Also note that the above call does not require admin rights, so it can be called in Visual Studio's Pre-Build Step or similar.