Unless your batch file is in a folder in the %PATH%
, PowerShell won't find it [1], so you'll have to supply an explicit file path (whether relative or absolute).
例如,如果批处理文件位于当前文件夹中,请运行:
powershell -c ".\my.bat | tee output.log"
Consider adding -noprofile
to suppress loading of the profile files, which is typically only needed in interactive sessions.
如果批处理文件路径包含嵌入空格,请将其用单引号括起来并在前面添加&
:
powershell -c "& '.\my script.bat' | tee output.log"
注意:我特意添加了-c
(短缺:-Command
) 上面的参数名称;尽管powershell.exe
- Windows PowerShell- 默认为该参数,但在以下情况下不再适用PowerShell [核心] v6+(其可执行文件名称是pwsh
), 在哪里-File
现在是默认值 - 请参阅about_PowerShell.exe https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_PowerShell.exe and about_pwsh https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_pwsh
[1] More accurately, PowerShell - unlike cmd.exe
- will by design not execute scripts in the current folder by their mere filename (in an interactive PowerShell session you'll get a hint to that effect). This is a security feature designed to prevent accidental invocation of a different executable than intended.