Search code examples
windowspowershellcommand-linecmdtortoisegit

How to pause long enough that cmd.exe exits?


I am updating TortoiseGit repository using the following code (which works) in command line file (MyTestRepo.cmd):

cd c:\MyTortoiseGitRepo
git.exe pull --progress -v --no-rebase "origin"

In PowerShell I am calling this file using the following code:

$TestPull = Start-Job { Invoke-Item C:\MyTests\MyTestRepo.cmd }
Wait-Job $TestPull
Receive-Job $TestPull

The above code does work but it is not waiting long enough for the CMD file to finish running and exit cmd.exe to exit before moving on to the next line of code.

What better way to you have to wait for the cmd.exe process to finish before moving on?


Solution

  • Invoke-Item doesn't support waiting. You can use the call operator &. Ex:

    $TestPull = Start-Job { & "C:\MyTests\MyTestRepo.cmd" }
    

    Or Start-Process -Wait:

    $TestPull = Start-Job { Start-Process -FilePath "C:\MyTests\MyTestRepo.cmd" -Wait }
    

    Start-Process will show the cmd-window when the script is excecuted by a user. This can be suppressed by adding -NoNewWindow`.