ai-coding-minesIndexGitHub

`2>&1` on a native exe: exit 0, but `$?` is false

Windows and PowerShell

Symptom

A native executable like git exits 0, yet $? is false and git's ordinary progress messages are spewed as red errors.

Cause

When you redirect a native exe's stderr with 2>&1, PS 5.1 wraps each line in an ErrorRecord. Writing anything to stderr is treated as failure. Plenty of CLIs send normal progress to stderr.

Fix

Don't put 2>&1 on native commands. If you must swallow output, wrap it:

try { & $exe @args 2>&1 | Out-Null } catch { }

Verification

Look at $LASTEXITCODE. For native commands, that is the success signal, not $?.