MoonshotAI / MoonshotAI/kimi-cli

Feature Request: Support PowerShell 7 (pwsh.exe) on Windows

Open
#1,944 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
11.4k
Forks
1.3k
Avg merge
9h 47m
Merged PRs (30d)
2

Description

Feature Request: Support PowerShell 7 (pwsh.exe) on Windows

Summary

On Windows, the Shell tool is hardcoded to use Windows PowerShell 5.1 (C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe). It does not attempt to locate or use PowerShell 7 (pwsh.exe), which is the modern, cross-platform, actively maintained version of PowerShell.

Problem

1. PowerShell 5.1 Pipeline BOM Bug

Windows PowerShell 5.1 injects a UTF-8 BOM (EF BB BF) at the beginning of stdin when piping strings to external processes. This breaks tools that expect clean JSON or raw byte streams via stdin.

Reproduction:

# In Windows PowerShell 5.1
echo '{"test":1}' | python -c "import sys; print(sys.stdin.buffer.read()[:10].hex())"
# Output: efbbbf7b2274657374...  <-- BOM present
# In PowerShell 7
pwsh -Command "echo '{\"test\":1}' | python -c \"import sys; print(sys.stdin.buffer.read()[:10].hex())\""
# Output: 7b2274657374223a31...  <-- No BOM
2. Real-world Impact

When using Unity MCP tools (unity-mcp-cli) which accept JSON via --input-file - (stdin), the BOM causes JSON parse failures:

ERROR: --input-file content from 'stdin' must be valid JSON
Invalid JSON: Unexpected token '', "{"test":..." is not valid JSON

Users are forced to use temporary file workarounds instead of clean stdin pipes.

Proposed Solution

Modify kimi_cli/utils/environment.py to detect pwsh.exe before falling back to powershell.exe:

if os_kind == "Windows":
    possible_paths = []
    
    # 1. Prefer PowerShell 7 (pwsh) if available
    program_files = os.environ.get("ProgramFiles", r"C:\Program Files")
    program_files_x86 = os.environ.get("ProgramFiles(x86)", r"C:\Program Files (x86)")
    local_appdata = os.environ.get("LOCALAPPDATA", r"%LOCALAPPDATA%")
    
    pwsh_candidates = [
        KaosPath(os.path.join(program_files, "PowerShell", "7", "pwsh.exe")),
        KaosPath(os.path.join(program_files_x86, "PowerShell", "7", "pwsh.exe")),
        KaosPath(os.path.join(local_appdata, "Microsoft", "WindowsApps", "pwsh.exe")),
        KaosPath("pwsh.exe"),  # Let PATH resolve it
    ]
    
    for path in pwsh_candidates:
        if await path.is_file():
            shell_name = "pwsh"
            shell_path = path
            break
    else:
        # 2. Fallback to Windows PowerShell 5.1
        shell_name = "Windows PowerShell"
        system_root = os.environ.get("SYSTEMROOT", r"C:\Windows")
        win_ps_path = KaosPath(
            os.path.join(system_root, "System32", "WindowsPowerShell", "v1.0", "powershell.exe")
        )
        if await win_ps_path.is_file():
            shell_path = win_ps_path
        else:
            shell_path = KaosPath("powershell.exe")

And update kimi_cli/tools/shell/__init__.py to handle the new shell_name:

def _shell_args(self, command: str) -> tuple[str, ...]:
    if self._is_powershell:
        return (str(self._shell_path), "-command", command)
    return (str(self._shell_path), "-c", command)

(Note: Both pwsh and powershell use -command, so _shell_args may not need changes if shell_name is checked loosely.)

Alternative: Environment Variable Override

If automatic detection is undesired, consider supporting an environment variable:

KIMI_CLI_SHELL_PATH="C:\Program Files\PowerShell\7\pwsh.exe"

This would allow advanced users to override the shell without code changes.

Environment

  • OS: Windows 10/11
  • Kimi CLI Version: Latest (installed via uv)
  • PowerShell 7 Location: C:\Program Files\PowerShell\7\pwsh.exe
  • Windows PowerShell Location: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Additional Context

PowerShell 7 is the recommended version for all new development. It fixes numerous bugs present in 5.1 (including the stdin BOM issue) and provides better cross-platform compatibility. Many developers on Windows have both installed, with pwsh being their primary interactive shell.


Thank you for considering this improvement!
上面都是kimi code帮忙写的

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in kimi_cli/utils/environment.py, where Windows shell paths are selected, and inspect kimi_cli/tools/shell/init.py for PowerShell command arguments. Reproduce the stdin behavior on Windows with PowerShell 5.1 and 7, then verify that pwsh.exe is preferred when available, powershell.exe remains the fallback, and shell commands still receive the correct arguments.

Written by the indexing model from the issue text.

Assessment

Tech stack
powershell, python
Domain
cli, operating-systems
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.