【发布时间】:2021-01-07 23:20:39
【问题描述】:
我运行这样的 powershell 命令
powershell.exe -File C:\GitHub\project\test\run.ps1 "foo bar"
我的当前目录已经是C:\GitHub\project\test。如何使-File 参数仅从当前目录开始?
谢谢
【问题讨论】:
标签: powershell command-line-interface
我运行这样的 powershell 命令
powershell.exe -File C:\GitHub\project\test\run.ps1 "foo bar"
我的当前目录已经是C:\GitHub\project\test。如何使-File 参数仅从当前目录开始?
谢谢
【问题讨论】:
标签: powershell command-line-interface
只需完全省略目录路径 (powershell.exe -File run.ps1 "foo bar") 或在脚本文件名前加上 .\ (powershell.exe -File .\run.ps1 "foo bar") 即可运行位于当前目录中的脚本。
请注意,如果您需要使用-Command 而不是-File,.\ 前缀是必需的,因为 PowerShell 然后将 CLI 参数视为 PowerShell 代码,出于安全原因,PowerShell 不允许仅通过文件 name 在当前目录中运行脚本 - 请参阅 this answer。
参见:about_PowerShell_exe,Windows PowerShell CLI 的文档(PowerShell [Core] v6+,其可执行文件名为pwsh,相关主题为about_pwsh。)
【讨论】: