在PowerShell脚本中从给定文件夹运行命令提示符(cmd)

3

有没有可能从不同的文件夹(例如C:\ScriptHome)运行cmd命令,而不是脚本主目录所在的文件夹?

我的意思是,例如:

Cmd /C "C:\Test\test.exe"
  • 但是这个exe应该从例如"C:\RunTestExeHere"中调用

基本上,可以在纯cmd中完成,比如cd "C:\RunTestExeHere",之后执行

C:\RunTestExeHere>C:\Test\test.exe
  • 但是它是否可以在PowerShell中完成呢?

谢谢!

1个回答

2

最好的方法是在Push-LocationPop-Location命令之间嵌入您的外部命令。

一个简单的例子:

Push-Location -EA Stop C:\  # You'd use C:\RunTestExeHere instead
cmd /c dir                  # You'd use & "C:\Test\test.exe" instead
Pop-Location 

另一种选择(调用语法不太方便):
Start-Process -Wait -NoNewWindow cmd -ArgumentList /c, dir -WorkingDirectory C:\

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接