H02
A PowerShell hook called with -Command does not block Claude Code
Symptom
Your hook is a PowerShell script that exits 2 to deny, yet writes go through. Run by hand, the script does return 2.
Check it in 30 seconds
Paste into Windows PowerShell 5.1. It only creates files under your %TEMP% folder. If the last line says REPRODUCED, it happens on your machine. It calls claude once, so it takes about a minute.
$d = Join-Path $env:TEMP ('cc-trap-' + (Get-Date -Format 'yyyyMMddHHmmss')); New-Item -ItemType Directory $d -Force | Out-Null
[IO.File]::WriteAllText("$d\deny.ps1", '[IO.File]::WriteAllText(''MARK'', ''x''); [Console]::Error.WriteLine("denied"); exit 2'.Replace('MARK', "$d\called.txt"))
$hook = 'powershell -NoProfile -ExecutionPolicy Bypass -Command \"& ''' + ($d -replace '\\', '/') + '/deny.ps1''\"'
[IO.File]::WriteAllText("$d\s.json", '{"hooks":{"PreToolUse":[{"matcher":"Write|Edit","hooks":[{"type":"command","command":"HOOK"}]}]}}'.Replace('HOOK', $hook))
Push-Location $d
'Use the Write tool once to create the file written.txt with the content hi. Do not retry if it is blocked.' | claude -p --settings s.json --permission-mode acceptEdits | Out-Null
Pop-Location
$w = Test-Path "$d\written.txt"; $c = Test-Path "$d\called.txt"
"deny hook called through -Command: hook called=$c, written=$w"
if (-not $c) { 'CANNOT CHECK (the hook was never called: check disableAllHooks or your settings)' } elseif ($w) { 'REPRODUCED' } else { 'NOT REPRODUCED' }
Why it happens
A hook denies with exit code 2. Called as powershell -Command "& 'deny.ps1'", the 2 becomes 1 (same as T01), and 1 is a non-blocking error, so the write proceeds. On Windows without Git Bash a failure can block instead, so this is not expected there (official reference; the author's machine has Git Bash).
Fix
Call PowerShell hooks with -File. Also add "onFailure": "block" (it blocked even with -Command).
"command": "powershell -NoProfile -ExecutionPolicy Bypass -File C:/hooks/deny.ps1",
"onFailure": "block"
On the author's machine
-File blocked / -Command let the write through / -Command with onFailure: block blocked
Author's machine: Windows 11 / Windows PowerShell 5.1.26100 / Python 3.13.7 / Claude Code 2.1.304, Japanese Windows (code page 932). Checked 2026-10-20 to 22.
If you searched for
Related
cc-win-kit bundles the parts that avoid these traps from the start: a write guard (with onFailure: block), an unattended runner, a doctor that scans your files and hook settings, CLAUDE.md templates and the full test suite.
Other symptoms
- PowerShell returns exit code 1 when the script exited with 2
- The second value passed with -File disappears without an error
- With -File, a,b stays one string instead of an array
- Non-ASCII text piped to claude -p, python or git arrives as ?
- Output from python or claude turns into mojibake in a variable
- Python works when run by hand but crashes under Task Scheduler or redirection
- PYTHONIOENCODING is set, but python -I ignores it
- A .ps1 with non-ASCII text fails to parse or prints mojibake
- A .cmd / .bat file runs its comment text as commands
- $variables in a powershell -Command one-liner vanish before it runs
- Get-ChildItem -Include is ignored and every file comes back
- Markdown backticks inside "..." turn into newlines and tabs
- A log that is being written shows 0 bytes
- claude -p --allowedTools swallows the prompt after it
- A Claude Code PreToolUse hook lets the write through when it times out
- No Claude Code hook runs at all (disableAllHooks is still set)