Wait for Command to Finish in PowerShell

Wait for Command to Finish in PowerShell

Using Start-Process Cmdlet

Use Start-Process with the -Wait parameter to wait for the command to finish.

OUTPUT:

powershell wait for command to finish - wait parameter

In the above output, we can see that the command waits until the file explorer is opened. To do that, we used Start-Process with the -Wait parameter. The Start-Process cmdlet is used to start one or multiple processes on the local machine. By default, it creates a new process, which inherits every environment variable defined in the current process.

Next, we specified the process name preceded by the Start-Process cmdlet. We can specify a script, executable, or file that will be opened using a program on our local computer. Remember, if we mention a non-executable file, then the Start-Process cmdlet will start the program associated with the given file.

The Start-Process lets us use various options and parameters; one of them is the -Wait parameter, which means this particular cmdlet will wait for the mentioned process, including its descendants, to finish before getting more inputs. This parameter will retain a window or suppresses the command prompt until the specified process finishes.

We can also set a timer to wait before proceeding with the following command. This is where the Timeout command comes into the picture. See the following example to learn how to use the Start-Process cmdlet with the Timeout command.

Use Start-Process with the Timeout command to wait for a specific time frame to end before executing the next command.

OUTPUT:

powershell wait for command to finish - timeout parameter

Here, we started the process and ran the Write-Host cmdlet after waiting for 5 seconds. You can see the countdown on the left side of the PowerShell console. For the above code example, it is essential to understand the flow of execution. First, the process was started using the Start-Process cmdlet. What is this cmdlet, and why do we use it? We have already learned this using the -Wait parameter above.

Second, the Timeout command was executed to wait for the given number of seconds. Once the timer was over, the Write-Host cmdlet was executed, which displayed a message on the PowerShell console. Finally, the Write-Host cmdlet was used to write a customized output/message to the host; here, the host means PowerShell console.

If you are using Windows PowerShell version 5.0 or above, you must know that the Write-Host is the wrapper for the Write-Information. So what is the advantage of that? This lets us use the Write-Host to emit outcome to an information stream, which enables the suppression or capture of data written using the Write-Host cmdlet while preserving backward compatibility.

Using Start-Sleep Cmdlet

Use the Start-Sleep cmdlet with the -Seconds parameter to wait for the given time frame before we continue executing the following commands in PowerShell.

OUTPUT:

powershell wait for command to finish - start sleep cmdlet

As you can see in the above output, we have to wait for 10 seconds to finish before executing the Write-Host cmdlet. We have covered Write-Host in the previous section; let’s continue with the Start-Sleep cmdlet. This cmdlet was used to suspend the session or activity for the given time. We can use it based on our needs, such as it can be used to wait for a particular operation to finish or before repeating the operation.

The -Seconds was used to specify how long a particular resource has to sleep (in seconds). We can omit this parameter name or make it shorter and use -s. Note that we can specify fractional values using PowerShell version 6.2.0 or higher.

Using Wait-Job Cmdlet

To wait for the background job to end:

  • Use Start-Job to create and run a background job.
  • Use the Wait-Job cmdlet to wait for a background job to complete.
  • Once the background job is completed, use Write-Host to print a customized message on the console.

OUTPUT:

powershell wait for command to finish - wait job cmdlet

In the above script, we used the Start-Job to create and run a background job. This raised two points: first, what is a background job and second, what commands are included to accomplish this job? A background job means a command or set of commands that run in a separate process in our local machine. The script block represented with curly braces ({}) contains all the commands that will be executed as part of this background job.

In our case, we used a while loop which starts iterating from 0 (including) to 10 (excluding). To repeat, we set a counter variable named $i to 0 and increment it by 1 on each iteration. Note that we wait for 3 seconds before moving to the next iteration.

Next, we used the Wait-Job cmdlet to wait for the background job to finish. Here, the $startJob.Name contained the background job’s name created by the Start-Job cmdlet; this name was then passed (as an argument) to the Wait-Job cmdlet to wait for this job to complete.

Finally, we used the Write-Output cmdlet to print a string message on the console and to demonstrate that this command will only be executed after completing the background job. See the above output for a better understanding.

Using Wait-Process Cmdlet

Use the Wait-Process cmdlet to wait for the command to finish in PowerShell.

OUTPUT:

powershell wait for command to finish - wait process cmdlet

Here, we used the Wait-Process cmdlet to wait for the process/processes to end. For the above example, it waits for the NotePad to be closed before accepting more inputs or moving to the following command in the pipeline. This cmdlet suppresses the command prompt until the process is finished/stopped. We can also mention the process using process ID (also referred to as PID), process name or pipe it to the Wait-Process cmdlet. Note that we can use this cmdlet for processes that are running locally on our machine.

The Wait-Process cmdlet will not work on macOS and Linux.

Using Diagnostics.Process Class

Use the Diagnostics.Process class to wait for the command/process to finish in PowerShell.

OUTPUT:

powershell wait for command to finish - diagnostics process class

The working of the Diagnostics.Process class is the same as the Wait-Process cmdlet that we learned in the previous section. We can prefer it if we have a .NET library installed on our machine. However, struggling with a challenging solution is not recommended when we already have simple and easy PowerShell commands. Concerning the above solution, the Start() method started the given process we passed as an argument while WaitForExit()waited for this process to end.

Using Out-Null Cmdlet

Use the Out-Null cmdlet to wait for the command/process to finish in PowerShell.

OUTPUT:

powershell wait for command to finish - out null cmdlet

Using Out-Null is another solution to wait for the specified process to end/exit. Pipping with the Out-Null cmdlet means we are ticking the Windows PowerShell into running the underlying process on top of a running application. This cmdlet does not display anything but waits for the command/process to complete. Remember, Out-Null will wait for the whole command to execute.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *