If you're looking to get mock code which performs the same function as $?
, you can use something like this (it is quite limited in terms of how it actually returns if multiple lines are executed at once, etc., and it will likely need to be modified depending on the execution context):
Function Test-LastCommandError {
$LastCommand = (History | Select -Last 1).CommandLine
$LastError = $Error[-1].InvocationInfo.Line
$LastCommand -eq $LastError
}
1/1#Success
Test-LastCommandError # Returns false
1/0#Error
Test-LastCommandError # Returns true
This works for me manually executing each line, but not in an ad-hoc ISE window (since it copy-pastes everything as one command on execute).