You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell实现类似bash cat -的stdin持续重定向到stdout方案咨询

PowerShell 下实现类 cat - 标准输入全量重定向方案

核心问题原因

  • PowerShell 中的 cat 是 Get-Content 的别名,设计上仅用于读取指定路径的文件内容,不会主动读取当前进程的标准输入流,和 Unix 体系下 cat - 读取 stdin 的语义完全不同
  • Windows 系统的 stdin 分为控制台键盘输入、管道输入、文件重定向输入等多种类型,需要直接调用系统级输入接口才能兼容全场景

适配跨平台脚本的解决方案

你可以直接在 PowerShell 脚本段中使用如下命令替换 cat -,实现后续所有输入内容全部重定向到 stdout,且不会被 PowerShell 解释器执行:

$input | Out-Default; exit

完整脚本示例

# 前置执行指令
(New-Object Media.SoundPlayer "rick.wav").PlaySync()
# 全量读取剩余stdin输出到stdout,直接终止后续代码解析
$input | Out-Default; exit
random text
that will be redirected to stdout
powershell will never parse or execute this part

执行命令说明

使用管道传入脚本时,需使用 powershell -Command - 模式执行,不要用 iex 逐行解析,避免截断输入:

# 本地文件执行
Get-Content test.ps1 -Raw | powershell -NoProfile -Command -

# curl拉取远程脚本执行
curl https://your-script-url | powershell -NoProfile -Command -

全stdin类型兼容方案

如果需要同时兼容键盘交互式输入、管道输入、文件重定向输入等所有Windows stdin类型,可以使用如下命令替代,实现和 cat - 完全一致的效果:

while($null -ne ($line = [Console]::In.ReadLine())) { Write-Output $line }

内容的提问来源于stack exchange,提问作者Jesse Taube

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.08 02:54:02