[BUG] PowerShell wrapper fails with "StandardOutputEncoding is only supported when standard output is redirected" error
Environment
- Platform (select one):
- [x] Anthropic API
- [ ] AWS Bedrock
- [ ] Google Vertex AI
- [ ] Other: <!-- specify -->
- Claude CLI version: 1.0.77 (Claude Code)
- Operating System: Microsoft Windows 10.0.26100
- Terminal: PowerShell Core 7.5.2
Bug Description
The claude.ps1 wrapper script fails with "StandardOutputEncoding is only supported when standard output is redirected" error when called by other PowerShell tools that attempt to capture output. This prevents Claude Code from being used in automated workflows or PowerShell-based tooling.
Steps to Reproduce
- Install Claude Code on Windows
- Create or use a PowerShell script that calls
claudeand captures its output - Execute the script - the encoding error occurs consistently
- Error specifically happens at line 24 in claude.ps1 during the
& "node$exe"invocation
Expected Behavior
Claude should execute successfully when called from other PowerShell scripts and tools, allowing output to be captured without encoding errors.
Actual Behavior
Execution fails with:
Program 'node.exe' failed to run: StandardOutputEncoding is only supported when standard output is redirected.At C:\Users\tot\AppData\Roaming\npm\claude.ps1:24 char:5
+ & "node$exe" "$basedir/node_modules/@anthropic-ai/claude-code/cl …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.
Additional Context
Root Cause: PowerShell attempts to set StandardOutputEncoding when it detects potential output redirection, but fails when actual redirection isn't properly configured in the wrapper script.
Proposed Fix: Add 2>&1 redirection to all & invocations in claude.ps1 and wrap in try/catch blocks:
#!/usr/bin/env pwsh
$basedir = Split-Path $MyInvocation.MyCommand.Definition -Parent
$exe = ""
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
# Fix case when both the Windows and Linux builds of Node
# are installed in the same directory
$exe = ".exe"
}
$ret = 0
try {
if (Test-Path "$basedir/node$exe") {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "$basedir/node$exe" "$basedir/node_modules/@anthropic-ai/claude-code/cli.js" $args 2>&1
} else {
& "$basedir/node$exe" "$basedir/node_modules/@anthropic-ai/claude-code/cli.js" $args 2>&1
}
$ret = $LASTEXITCODE
} else {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "node$exe" "$basedir/node_modules/@anthropic-ai/claude-code/cli.js" $args 2>&1
} else {
& "node$exe" "$basedir/node_modules/@anthropic-ai/claude-code/cli.js" $args 2>&1
}
$ret = $LASTEXITCODE
}
} catch {
Write-Error "Claude execution failed: $($_.Exception.Message)"
$ret = 1
}
exit $retThis issue has 4 comments on GitHub. Read the full discussion on GitHub ↗