【问题标题】:Run cmd from a given folder inside PowerShell script从 PowerShell 脚本中的给定文件夹运行 cmd
【发布时间】:2016-10-25 13:53:17
【问题描述】:

是否有可能从与脚本主位置不同的文件夹(例如 C:\ScriptHome)运行 cmd 命令?

我的意思是例如

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

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

C:\RunTestExeHere>C:\Test\test.exe
  • 但是可以在 powershell 中完成吗?

谢谢!

【问题讨论】:

    标签: powershell cmd


    【解决方案1】:

    最好的办法是将外部命令夹在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:\
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-13
      • 2016-07-13
      • 2018-08-14
      • 1970-01-01
      • 2018-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多