My goal is copying an .exe file from a bin folder of solution A to another solutions B resource folder on the post build event of solution A.
I created the necessary xcopy command and tried it out in powershell: It works perfectly.
Whenever I add any command to the actions in VS build fails with: "#command# exited with code 1", where #command# is, for example, the xcopy command.
I tried running VS as admin and currently I tried just running a .bat file that contains "@echo off @exit 0". That too leads to "#command# exited with code 1".
Have some example of what I tried out as VS post/pre-build command:
call "projdir\test.bat"
call projdir\test.bat
"projdir\test.bat"
projdir\test.bat
... I tried projdir as "$(ProjectPath)" and manual path.
I put output to verbose and found the following:
The command "C:\Users\Traubenfuchs\AppData\Local\Temp" is written incorrectly or couldn't be found. (That folder actually exists but I don't know what it wants to do it.)
The same thing happens when I put an xcopy command in pre/post build.
Anyone knows what I am doing wrong?

- 1,911
- 3
- 20
- 37
-
run as administrator solved my issue – Sreekumar P Aug 24 '15 at 07:30
3 Answers
Recently I met similar problem.
About your "hello world" post-build event : try to call powershell directly
powershell -command "write-output 'hello world'; exit 0"
=================
My pre-build event looks like :
powershell -file scriptPath.ps1
My scriptPath.ps1 looks like :
<my epic powershell code>
...
exit 0
Note that without "exit 0" at the end I recieved return code 1 from my script.

- 146
- 1
- 5
We had this issue because of a "Software Restriction Policy" set up in a domain GPO. It appears that pre and post builds create a .cmd batch file in the temp folder. Here's what the logging showed me:
cmd.exe (PID = 8456) identified C:\Users\brian\AppData\Local\Temp\tmp733425d2c0604973a90a0a175c13353e.exec.cmd as Disallowed using default rule, Guid = {11015445-d282-4f86-96a2-9e485f593302}
To fix this we modified the GPO that controlled the SRP so that it did not include .cmd files. After all, the goal of SRP is to block executable malware, not batch files. I may get some security blowback because of this. Hopefully someone knows a better way to fix this issue.

- 4,655
- 2
- 27
- 35
-
1
-
@briddums I believe I did this: HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\Safer\CodeIdentifiers\LogFileName="C:\logs\srp.log" – Brain2000 Jun 10 '17 at 02:48
-
unfortunatelly I am not able to modify that policy, and none of other options presented here work... – Johnny Jul 21 '17 at 08:05
One possible issue is that you have to use $(ProjectDir)
instead of $(ProjectPath)
When you use $(ProjectPath)
it is actually: "C:\Users\....\Project\MyProject.csproj"
Versus $(ProjectDir)
which is: "C:\Users\....\Project\"
Notice, that the first version is actually pointing to your project solution file... which would cause everything to fail.
Another is that that $(ProjectDir)
automatically adds the trailing slash to the path. i.e. "$(ProjectDir)\test.bat"
would actually translate to: "C:\Users\....\Project\\test.bat"
Also you have to make sure that you enclose all your file paths in double quotes
So the correct call would look like this:
call "$(ProjectDir)test.bat"
Other things to check out would be to make sure that the directory that the script is executing from is correct. See SO: visual studio 2012, postbuild event, bat file not creating new file (not executing)
Have you checked out the suggestions in this StackOverflow? Post Build exited with code 1
Edit
Try this:
echo Hello World
@exit 0
You need to end any commands with @exit 0
otherwise it doesn't think that it finished properly
Edit 2
Why do we use @exit 0
instead of exit 0
? @Sunny has a good explanation of the At symbol - @ here:
The @ symbol tells the command processor to be less verbose; to only show the output of the command without showing it being executed or any prompts associated with the execution. When used it is prepended to the beginning of the command, it is not necessary to leave a space between the "@" and the command.
Essentially if you call xyz.bat
by default every command in the .bat
file is echoed back along with the actual output of the command. e.g.:
test.bat
echo Hello World
exit 0
Will output:
C:\>call test.bat
C:\>echo Hello World
Hello World
C:\>exit 0
C:\>
When we add a @
in front of the commands, we only get the output of the commands themselves.
test2.bat
@echo Hello World
@exit 0
Will output:
C:\>call c.bat
Hello World
C:\>
@echo off
is also a way to turn this off for the rest of the script. We typically do this simply to make the logs easier to read, as the text of the commands muddies the log output.

- 12,384
- 1
- 34
- 46
-
I tried all that but it didn't help. Take a look at the image I posted, I can't even call echo. I think I am doing something wrong fundamentally. – ASA Jul 31 '14 at 11:57
-
1Try adding `@exit 0` to the end of your commands (see edit) otherwise it will always think that something went wrong. – HAL9256 Aug 19 '14 at 23:27
-
1No luck: The command "echo Hello World @exit 0" exited with code 1. C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets 1063 5 SOLUTIONNAME – ASA Aug 20 '14 at 07:19
-
I think you have a typo: _Another is that $(ProjectDir) automatically adds the trailing slash to the path. i.e. "$(ProjectDir)\test.bat" would actually translate to: "C:\Users....\Project\test.bat_ should be _Another is that $(ProjectDir) automatically adds the trailing slash to the path. i.e. "$(ProjectDir)\\test.bat" would actually translate to: "C:\Users....\Project\test.bat"_ (double final "\"). – SteveCinq Jan 10 '18 at 05:12
-
`ECHO|SET /P="My final line without a CR">> "$(ProjectDir)myfile.vb" @EXIT 0` worked for me. – SteveCinq Jan 10 '18 at 05:18
-
Does anyone know if exit 0 accomplishes the same thing as @exit 0 ? exit 0 worked for me. – Waldron Aug 27 '21 at 13:32
-
1@Waldron. Good question. Both `@exit 0` and `exit 0` work the same, the only difference is in the generated output (see Edit 2 above). – HAL9256 Aug 27 '21 at 16:49