Powershell bitwise comparison for robocopy exit codes


I can’t get my head around this bitwise conversion problem.

Robocopy exit codes don’t conform to the normal 0(success), 1(failure) pattern so I want to wrap my robocopy calls in the powershell script below to make my TeamCity build configurations fail or proceed appropriately when robocopy terminates.

That first part was solved using a tip from the net with: ($LastExitCode -band 24) which correctly treats exit codes 8 through 16 as failures (1) and all others as success (0).

Now I want to echo a message corresponding to the exit code. How do I convert and round/floor the integer exit code (0 – 16) to its hexadecimal equivalent (0x00 – 0x10)?


In your case you do not need to convert it.
You do not need conversion because Hashtable keys are converted to [int] on pre-compiation stage.
If you lookup for $returnCodeMessage.Keys , you will see decimal numbers, not hex numbers

To display all messages you should use

IF you want to show Hex-encoded $LastExitCode, do

String System.Convert.ToString(Int32, Int32)


Powershell bitwise comparison for robocopy exit codes by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply