Powershell

How to Wait for Command to Finish in PowerShell

PowerShell commands provide extensive functionality support to perform various tasks. PowerShell supports many cmdlets and functions that are designed to do a dedicated job. There are several cmdlets that refer to the wait process in PowerShell. The PowerShell wait for a command to finish support enables you to stop/halt the process, job or command. This article explains the PowerShell cmdlets that are used to stop or wait for the command to finish.

How to wait for the command to finish in PowerShell

The PowerShell waiting process for commands is supported by several commands. This section describes the know-how of those commands and a brief explanation.

Using the wait parameter
As the name of the parameter suggests, the -Wait parameter belongs to the common-parameters family of PowerShell and thus can be used with various cmdlets. The command written below practices the use of the wait parameter with the “Start-Process” cmdlet.

> Start-Process -FilePath "E:\linuxhint\test.txt" -Wait

The -Wait parameter will not allow you to execute any other command until the first process terminates.

Using the Wait-Process cmdlet
The Wait-Process cmdlet of PowerShell handles the process-related execution in PowerShell. The Wait-Process cmdlet waits for the process to stop/finish before moving towards the next command. When a process is handled using the Wait-process command, then you would not be able to use the PowerShell console until the specified process is not terminated. For instance, we have applied the Wait-Process cmdlet to the “chrome” process. The cursor will keep on ticking until all the instances of chrome are closed:

> Wait-Process -Name chrome

You can perform the above-mentioned operation in the following way as well.

By using the command provided below, we have stored the information of chrome processes (Get-Process) in a variable $proc.

> $proc=Get-Process chrome

And then passed the $proc to the Wait-Process command with the InputObject parameter. The output shows that the terminal cannot be used for more commands until chrome is stopped or the chrome processes are finished.

$ Wait-Process -InputObject $proc

Using Timeout parameter
The -Timeout can be used as a join between two commands. This section provides a few commands that better clarify the usage of the Timeout command. As the name directs, the -Timeout parameter specifies the time limit for any command to execute. Once the specified time limit is crossed, PowerShell throws an error. For instance, the command provided below fixes the timeout limit to 5sec, after passing the 5 sec, the Wait-Process command automatically throws an error:

> Wait-Process -Name chrome -Timeout 5

The Timeout parameter command in PowerShell restricts the next command from execution.

Additionally, the Timeout operator can be used with various cmdlets and functions of PowerShell. For instance, the command provided below practices theĀ  -Timeout parameter with the Start-process cmdlet. As the value of the -Timeout parameter is 5, so, the second command will be executed after 5 seconds of the first command.

Note: During the countdown time of 5 sec, you can press any key to execute the second command at that time.

> Start-Process explorer; Timeout 5; Write-Host "The explorer process has been started"

Using the Start-Sleep cmdlet
The Start-Sleep cmdlet of PowerShell allows you to halt the processing in PowerShell for a specific amount of time. The Start-Sleep cmdlet accepts the time in milliseconds or seconds. The following command practices the use of the Start-Sleep command and the time is defined in seconds. The command will execute after passing the time stated in the Start-Sleep cmdlet.

> Start-Sleep -Seconds 10; Write-Host "command is executed after 10sec"

Conclusion

PowerShell does support several cmdlets and functions to accomplish different tasks automatically. These cmdlets and functions can be executed as per the requirement of the user. This article guides you to the possible ways that can be adopted to finish one command before executing the other. PowerShell supports several operators and cmdlets that can be used to wait for the command to finish. In this regard, we have experienced the working of cmdlets such as Wait-Process and Start-Sleep. We have also presented the functionality of the Timeout and -Wait parameters. These parameters can be associated with several functions in PowerShell to do the job.

About the author

Adnan Shabbir