Title: Powershell script from Visual Studio Post-build-event failing
Tags: visual-studio;powershell;64-bit
Question: Running a powershell script as a post-build event in Visual Studio fails despite the fact that that same script runs fine from the commandline. Why is this?
Comment: Sorry, I don't expect an answer to this question! I already have an answer, just it was hard to find and not on SO so I have put a question here and answered it myself!

Comment: The error for me was really obscure "The System cannot Execute the specified program"

Comment: It also was a kind of duplicate of [Post build event execute powershell](http://stackoverflow.com/questions/6500320/post-build-event-execute-powershell/6501719#6501719)

Comment: Any chance to show the script and explaining what you mean by *failing* (error message, actual behavior differs then expected behavior - in this case please describe both, something else)?


Here is another answer: (This thread is not new, but I got here from Google, so I thought sharing the solution I found would be interesting to others)
I tried changing the path to powershell.exe to &quot;%WINDIR%\SysNative\WindowsPowerShell\v1.0\powershell.exe&quot; and it worked perfect. The 64 bits version is called from the Post Build event and it successfully adds the SharePoint snapin.
Credits to this article: https://docs.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/ff798298(v=office.14), &quot;Using Windows PowerShell Scripts to Automate Tasks in Visual Studio&quot;.

Here is another answer: Answering my own question just to save others the pain of researching this.
Well http://www.vistax64.com/powershell/205436-running-powershell-post-build-event-ignoring-executionpolicy.html suggests that despite Visual Studio being 32 bit, it runs the 64 bit version of powershell which is independent.
The reality seems that Visual Studio runs the Windows\syswow64 version of Powershell (32 bit!?) while your normal Powershell command prompt will default to the Windows\system32 version (64 bit?!). You need to set execution policy etc for that separately.

Here is another answer: Sorry for re-animating an ancient question but since my search lead me to this post and my resolution was slightly different I wanted to add it.
In my case the issue was with Visual Studio 2017 CE on Windows 10 1903 and the symptom was a failure to run a powershell script as a post-build event even though the same powershell ran without error from the command line with the same privileges.
I was getting an exited with error 1 but could see the script was not actually being run.
VS was using the 32 bit version (not the 64 as per the accepted answer to the question) and it was that build of powershell that I'd not set the exectution policy on.
I ran PowerShell (x86) and Set-ExecutionPolicy Unrestricted, then my post-build tasks ran fine.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2011:00:10/30/11', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<visual-studio><powershell><64-bit>', 'Title': 'Powershell script from Visual Studio Post-build-event failing', 'ViewCount': 2170, 'id': 7947840, 'user_AboutMe': '', 'user_AccountId': 30913.0, 'user_DisplayName': 'Alan Christensen', 'user_Reputation': 787.0, 'user_Views': 135.0, 'user_id': 84590.0}}