$LastExitCode Not providing result


I would like to see if I could get some help on an error code. My final script will be to set an SMTP address for a mailbox, but I just used this code below as an example.

My expectation is that the first time I run the command, it does the work and I get successful error code. The second time I run the command, powershell comes back with the following.

I want to be able to get an error code for 3 situations. (It worked, It failed, and then the one above that it worked, but that no changes were made.) I just can’t seem to find any examples of this and would really appreciate some suggestions.



Ok, I got a great response and now I am using the following code. I have to modify thousands of objects and they will all be at different error codes, so I need to be able to write any of the conditions to a log file. I have a bunch of environment variables throughout the script, so in the end, I am going to put a “User Passed/Failed” $EmailAddress $DisplayName etc…..


Try this. It should exit with 0 if successful, 1 if successful but no changes, and 2 if it failed:


$LastExitCode Not providing result by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply