Many of you know the “timeout” command we used when we created Windows Batch scripts. This pauses/waits/sleeps the script for a specific amount of time. 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 complete or pausing before repeating an operation.
To sleep a PowerShell script for 5 seconds, you can run the following command
Start-Sleep -Seconds 5
You can also use the -milliseconds parameter to specify how long the resource sleeps in milliseconds.
Start-Sleep -Milliseconds 25
There are also aliases for the parameter such as -s and -ms if you don’t want to type out the full name. If you want to learn more about the Start-Sleep cmdlet, check out Microsoft Docs.
And I think many of you know, it is needless to say, that if you can avoid waits in a script, that’s a good thing. 😉
I hope that quick PowerShell post gives you an overview of how to add a sleep/wait/pause in a PowerShell Script. If you have any questions feel free to leave a comment. If you want to know more about how you can install and update to PowerShell 7, check out my blog post, you can also learn what is new in PowerShell 7 right here.
You can also learn about PowerShell remoting between Windows, Linux, and macOS using PowerShell remoting over SSH.Tags: command line, Pause, PowerShell, Powershell Script, script, Sleep, Start-Sleep, Suspend, timeout, Wait Last modified: January 22, 2021