PowerShell provides the Start-Sleep
command in order to pause or sleep the PowerShell execution for a specified time. For example, we may want to wait 5 seconds to make a request to the remote server for periodic checks, etc. In this tutorial, we examine how to sleep or wait in PowerShell.
Sleep For Seconds
The Start-Sleep command can be used to sleep PowerShell for the specified number of seconds. In the following example, we sleep the PowerShell execution for 5 seconds.
PS> Start-Sleep 5
Sleep for Milliseconds
Sometimes we may need to sleep in a more precise time like milliseconds. We can sleep in milliseconds of the PowerShell using the Start-Sleep command. We can provide the -Milliseconds
parameter to the Start-Sleep command. In the following example, we sleep for 500 milliseconds.
PS> Start-Sleep -Milliseconds 500
Alternatively, we can specify the millisecond delay as the second presentation. The 500 milliseconds is equal to 0.5 seconds. In the following example, we sleep the PowerShell execution for 500 milliseconds.
PS> Start-Sleep 0.5
Sleep In a Do-While Loop
We can sleep the PowerShell during usage of the Do-While loop. For example, we can make a request to the remote server with the function named Make_Request()
and then wait for 2.5 seconds.
Do {
Make_Request()
Start-Sleep 2.5
} While (Check_Response())