Wait for an Executable to finish

Again and again, one good thing with IT is that you have million ways to do the same thing. That’s right with PowerShell too.
This example is 3 ways to run an executable and wait for its completion before doing something else. Could be usefull to launch an msi setup and wait before tuning the freshly installed software.

  1. Start-Process -Wait
  2. Write-Host "1. Run the .NET framework installer and wait for its completion... (Start-Process -Wait)" -ForegroundColor Yellow
    $process="dotNetFx40_Full_x86_x64.exe"
    $args="/q /norestart"
    
    Start-Process $process -ArgumentList $args -Wait
    Write-Host "`nProcess `"$process`" has been executed and is now stopped." -ForegroundColor DarkGreen
    
  3. Call operator (&)
  4. Write-Host "2. Run the .NET framework installer and wait for its completion... (Call operator &)" -ForegroundColor Yellow
    $process="dotNetFx40_Full_x86_x64.exe"
    $args="/q /norestart"
    
    &$process $args | echo "Waiting"
    Write-Host "`nProcess `"$process`" has been executed and is now stopped." -ForegroundColor DarkGreen
    
  5. Background Job
  6. Write-Host "3. Run the .NET framework installer and wait for its completion... (Background Job)" -ForegroundColor Yellow
    $process="dotNetFx40_Full_x86_x64.exe"
    $args="/q /norestart"
    
    $job=Start-Job -filepath $process $args
    Wait-Job $job
    Receive-Job $job
    

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top