从命令提示符相对路径调用 Powershell

Call Powershell from Command Prompt Relative Path

我正在使用以下行从命令提示符调用 PS 脚本

powershell "& 'c:/script.ps1'"

但是我需要脚本相对于命令提示符 window。因此,如果命令提示符正在查看 C: 那么脚本实际上是

powershell "& 'script.ps1'"

有没有办法注入相对路径?

powershell "& '%cd%\script.ps1'"

或者如果您打算使用在 powershell 脚本中启动批处理文件的目录,那么您可以通过将此类行放在 PS 脚本的开头来使用参数...

param([string]$Directory)

然后从命令行调用填充它,例如...

Powershell C:\script.ps1 -Directory %cd%

要注入相对路径,您必须在批处理文件的起始位置写入

powershell "& '%~dp0\script.ps1'"