[BUG] Windows: PowerShell tool reports the exit code of an earlier native command even when the script ends successfully

Status Open
Reported on v2.1.281
Maintainer reply None cached
Activity 0 comments · opened Sep 27, 2026

Environment

  • Claude Code 2.1.281 (Claude desktop app 2.9939.2.0, MSIX), Windows 11 build 26200
  • PowerShell tool enabled, backed by PowerShell 7.6.6 (MSI install, C:\Program Files\PowerShell\7)

What happens

When a PowerShell tool command runs a native executable that exits non-zero, and later statements succeed, the tool still reports the whole call as failed with that earlier exit code. It looks like the wrapper returns whatever $LASTEXITCODE holds at the end of the script.

Repro (PowerShell tool):

cmd /c exit 3; 'done'

Actual: the output done is printed, but the tool result is marked as an error with Exit code 3.

Same command in plain PowerShell 7.6.6:

pwsh -NoProfile -Command "cmd /c exit 3; 'done'"
$LASTEXITCODE   # -> 0

Plain pwsh -Command returns 0 here, so this is not PowerShell's own exit-code behavior; it comes from how the tool wraps the command.

Why it matters

Common commands leave a non-zero $LASTEXITCODE behind without the overall task failing (robocopy uses 1–7 for success, git diff --quiet, findstr with no match, and so on). The model sees a "failed" call, often retries or rewrites a command that actually worked, and spends extra turns. In my own session logs, 57 of 83 PowerShell tool results marked as failed had succeeded, and this pattern was the main cause.

Expected

Either match pwsh -Command semantics (the exit status of the script as a whole, or $? of the last statement), or report the stale native exit code as a separate note instead of marking the whole call as an error.

Workaround we use

End commands with ; $rc=$LASTEXITCODE; if ($rc) { "exit=$rc" }; exit 0, so the value is printed and the call does not turn into a false failure.

View original on GitHub ↗