Powershell

Difference Between Start-Sleep and Wait-Process

In PowerShell scripting, there are numerous methods to optimize the scripts. For instance, PowerShell pausing methods are intended to pause the script for a specific time or to wait for some other process to terminate before continuing the execution. Start-Sleep and Wait-Process belong to the PowerShell pausing methods category.

Start-Sleep pauses the execution for a specified time. On the other hand, Wait-Process waits for some process to end and then continues the execution.

Considering their importance, we have compiled this guide to differentiate between Start-Sleep and Wait-Process cmdlets.

Difference between Start-Sleep and Wait-Process

The Wait-Process cmdlet waits for one process to stop before accepting another process, while the Start-Sleep cmdlet is utilized to suspend or pause the script for a certain period. For a better understanding, we have demonstrated the usage of Start-Sleep and Wait-Process explicitly.

How does Start-Sleep work in PowerShell?

There comes a time when we need to pause or suspend the script in PowerShell for a specific time period. There could be many reasons to pause the execution of the script. For instance, if you want to extend the execution time of the script or want to limit the time for taking input from the user.

Syntax

> Start-Sleep <-Seconds> <-Milliseconds> <-Parameters>

In the syntax:

  • The -Seconds and -Milliseconds refer to the units of time. The seconds should be in double datatype while milliseconds must be an integer value.
  • The <-Parameters> denotes the set of common parameters that are applicable with all the PowerShell cmdlets. The commonly used parameters include, Debug, ErrorAction, ErrorVariable, InformationAction, InformationVariable etc.

Example: Pause the script for a specific time
To apply the Start-Sleep, we have created an example script. The Start-Sleep is used between two Write-Host cmdlets in the following example code. The would-be a wait of 5 seconds after the first Write-Host and before the second Write-Host:

Write-Host "using Start-Sleep"
Start-Sleep -Seconds 5
Write-Host "The code after Start-Sleep"

Let’s execute the script:

The output shows that the line after the Start-Sleep cmdlet is executed after 5 seconds of wait.

How does Wait-Process work in PowerShell?

The Wait-Process cmdlet waits for one process to stop before going towards further execution of the script. By default, the Wait-Process will wait for an undefined time. However, you can limit the time for the waiting as well. The syntax of the Wait-Process is provided below:

Syntax

> Wait-Process <-Name> <-Time> <-Parameters>

In the syntax:

  • The -Name parameter refers to the name of the process.
  • The -Time parameter is used to limit the time of wait.
  • The -Parameters denote some of the commonly used parameters such as Debug, ErrorAction, etc.

Let’s understand it more clearly using the example.

Example: Wait for a process
As we know, the Wait-Process cmdlet waits for one process and then accepts another process. In the below example, the Wait-Process is applied on the Hp.MyHp process. Here, the Wait-Process cmdlet will wait for the Hp.MyHp process before continuing the further execution:

Write-Host “Using Wait-Process!
Wait-Process -ProcessName Hp.MyHP
Write-host “The Hp.MyHp process has been stopped!

Now, execute the script:

You will observe that the cursor will keep ticking and will not continue the further execution if the process is not stopped.

You can also specify the time in the Wait-Process and if the process is not stopped within the time limit, then PowerShell will throw an error. Let’s experience it via the following example, where we have limited the waiting time to 5 seconds:

Write-Host "Waiting for a process to stop!"
Wait-Process -ProcessName Hp.MyHP -Timeout 5

Let’s execute the script and look at the output:

It is observed that after waiting for 5 seconds, PowerShell threw an error that Hp.MyHp did not stop at the specified time.

Bonus-Point: The following table considers four primary factors to differentiate between the Wait-Process and the Start-Sleep cmdlet.

Factors Wait-Process Start-Sleep
Purpose Pause the script’s execution. Pause the Script’s execution.
Applicability Scope It is applied to wait for processes only. It can be applied to suspend the session/activity at any time. The scope of Start-Sleep is broader than Wait-Process.
Common Parameters Supports common parameters. Also supports common PowerShell parameters.
Time unit Seconds only. Seconds or Milliseconds.

You would have better understood the difference between the Wait-Process and the Start-Sleep in PowerShell.

Conclusion

Start-Sleep suspends or pauses the script for a certain period of time. In contrast, the Wait-Process cmdlet waits for one process before accepting another process. Both the methods are used as pausing methods in PowerShell. You have learned to apply Start-Sleep and Wait-Process cmdlet and their difference as well. Primarily, these cmdlets differ in the applicability scope. However, both serve the same purpose: to pause the PowerShell script’s execution.

About the author

Adnan Shabbir