Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell PowerShell to wait for each command to end before starting the next?

I have a PowerShell 1.0 script to just open a bunch of applications. The first is a virtual machine and the others are development applications. I want the virtual machine to finish booting before the rest of the applications are opened.

In bash I could just say "cmd1 && cmd2"

This is what I've got...

C:\Applications\VirtualBox\vboxmanage startvm superdooper     &"C:\Applications\NetBeans 6.5\bin\netbeans.exe" 
like image 266
John Mee Avatar asked Nov 16 '09 11:11

John Mee


People also ask

Does PowerShell wait for command to finish?

In addition, Windows PowerShell does not wait for the commands to finish when running executable programs. This article will show you ways to force the Windows PowerShell environment to wait for the commands with executable files to finish before moving to the following command.

How do you write a wait command in PowerShell?

In PowerShell, we can use the Start-Sleep cmdlet to suspend/pause/sleep/wait the activity in a script or session for the specified period of time. You can use it for many tasks, such as waiting for an operation to be completed or pausing before repeating an operation.

How do you make a PowerShell wait before closing?

PowerShell NoExit switch prevents the PowerShell console window from closing after running the script. When you double click on the PowerShell script file, or run with the PowerShell option, the script will execute quickly and then disappear.

How do you wait 5 seconds in PowerShell?

Using the PowerShell Start Sleep cmdlet You can also write Start-Sleep 5 to let the script sleep for 5 seconds.


2 Answers

Normally, for internal commands PowerShell does wait before starting the next command. One exception to this rule is external Windows subsystem based EXE. The first trick is to pipeline to Out-Null like so:

Notepad.exe | Out-Null 

PowerShell will wait until the Notepad.exe process has been exited before continuing. That is nifty but kind of subtle to pick up from reading the code. You can also use Start-Process with the -Wait parameter:

Start-Process <path to exe> -NoNewWindow -Wait 

If you are using the PowerShell Community Extensions version it is:

$proc = Start-Process <path to exe> -NoNewWindow -PassThru $proc.WaitForExit() 

Another option in PowerShell 2.0 is to use a background job:

$job = Start-Job { invoke command here } Wait-Job $job Receive-Job $job 
like image 169
Keith Hill Avatar answered Sep 29 '22 13:09

Keith Hill


Besides using Start-Process -Wait, piping the output of an executable will make Powershell wait. Depending on the need, I will typically pipe to Out-Null, Out-Default, Out-String or Out-String -Stream. Here is a long list of some other output options.

# Saving output as a string to a variable. $output = ping.exe example.com | Out-String  # Filtering the output. ping stackoverflow.com | where { $_ -match '^reply' }  # Using Start-Process affords the most control. Start-Process -Wait SomeExecutable.com 

I do miss the CMD/Bash style operators that you referenced (&, &&, ||). It seems we have to be more verbose with Powershell.

like image 36
Nathan Hartley Avatar answered Sep 29 '22 14:09

Nathan Hartley