PowerShell’s sleep command is Start-Sleep, which pauses a script or command session for a specified time. Use Start-Sleep -Seconds 5 to wait five seconds, or Start-Sleep -Milliseconds 500 to wait half a second.
How Start-Sleep works
Start-Sleep is a PowerShell cmdlet that suspends execution in the current runspace for a duration. While it is waiting, later commands in that script or pipeline do not run. Once the interval has elapsed, execution continues with the next command. The pause does not stop the computer, terminate PowerShell, or make the script wait for a particular program or event to finish.
Its common syntax is:
Start-Sleep -Seconds 5— pause for five seconds.Start-Sleep -Milliseconds 250— pause for 250 milliseconds.Start-Sleep 2— use the positional duration argument to pause for two seconds.
The command’s full name is Start-Sleep. PowerShell also accepts the shorter alias sleep, so sleep 5 works in an interactive PowerShell session. In scripts, using the full cmdlet name is often clearer because a reader can recognize what it does without knowing the alias.
Choose seconds or milliseconds
Use -Seconds when the intended delay is naturally measured in whole or fractional seconds. Use -Milliseconds when specifying a shorter interval as an integer is more readable. The parameters are alternatives: provide one duration, not both.
Start-Sleep -Seconds 1.5
Start-Sleep -Milliseconds 1500
Both examples request a pause of about one and a half seconds. For clarity, avoid mixing units mentally: 1,000 milliseconds equal one second. Decimal seconds are useful for expressing a fraction of a second, while milliseconds can make short waits easier to scan in code.
A delay is not a precision timing guarantee. Operating-system scheduling and other system activity can make the actual pause slightly longer than requested. Use Start-Sleep for ordinary delays, not for high-precision measurement, real-time control, or timing-sensitive coordination.
Use it in a script
Place the cmdlet between commands when you want a fixed delay before the next step:
Write-Output "Starting the next step"
Start-Sleep -Seconds 3
Write-Output "Three seconds have passed"
It can also be used inside a loop. This example checks a condition periodically rather than continuously:
while (-not (Test-Path "C:\Temp\ready.txt")) {
Start-Sleep -Seconds 2
}
Write-Output "The file is available"
The loop waits two seconds between checks, then continues after the file appears. This is called polling. Choose an interval that suits the task: a very short interval causes frequent checks, while a long one can make the script slow to notice a change. For an important production workflow, consider whether the application offers a proper event, completion signal, or readiness check instead.
For a simple countdown, the value can be updated on each pass:
for ($i = 5; $i -gt 0; $i--) {
Write-Output $i
Start-Sleep -Seconds 1
}
Here the script outputs the numbers 5 through 1, waiting one second after each output. Since the pause follows the output, the total run time includes five waits.
Sleep is not the same as waiting for a process
A frequent misconception is that sleeping gives another program time to finish. It only waits for a set duration; it does not determine whether the program has completed. If a process might finish sooner or later than expected, a fixed delay can either waste time or let the script proceed too early.
To wait for a process to exit, use a process-waiting command or mechanism instead. For example, Wait-Process -Id 1234 waits for the process with that ID to end. When starting a process, PowerShell’s Start-Process command can also wait for the launched process when used with its -Wait option. These approaches wait on process state rather than guessing a delay.
Likewise, if the goal is to wait for a file, service, network response, or application readiness, check that specific condition in a loop or use an appropriate event-based mechanism. A sleep may be useful between checks, but it is not proof that the condition has become true.
Common problems and practical limits
- The script appears frozen: That is expected during the pause. Check the requested duration and whether the cmdlet is inside a loop that repeats.
- The script carries on too early: A fixed pause may be shorter than the operation takes. Wait for the actual process or condition instead.
- The delay is longer than expected: Scheduling and system load can add time. Do not rely on it for exact timing.
- An unfamiliar command name causes confusion:
sleepis an alias in PowerShell, whileStart-Sleepis the cmdlet’s full name. Using the full name improves portability and readability in PowerShell scripts. - A long sleep blocks that script’s progress: Commands later in the same execution flow wait too. A pause does not make that flow perform other work in the meantime.
For an interruptible interactive command, pressing Ctrl+C commonly stops the current operation. In a larger script, interruption behavior depends on how the script is run and what else it is doing; handle cancellation deliberately if the script must clean up resources.
Frequently Asked Questions
What is the PowerShell equivalent of the sleep command?
Use Start-Sleep. For example, Start-Sleep -Seconds 5 pauses the current execution flow for five seconds.
Can I just type sleep in PowerShell?
Yes. sleep is an alias for Start-Sleep in PowerShell. The full name is usually clearer in scripts and easier for unfamiliar readers to understand.
How do I sleep for milliseconds in PowerShell?
Use the milliseconds parameter, such as Start-Sleep -Milliseconds 250 for a quarter-second pause.
How do I wait for a program to finish?
Wait for the process itself rather than sleeping for an estimated duration. For an existing process, Wait-Process can wait for it to exit; when launching a process, Start-Process -Wait can wait for completion.
Does Start-Sleep use CPU while waiting?
It suspends the current execution flow rather than repeatedly running your next script commands. The operating system still schedules other work; the cmdlet is not a substitute for a precise timer or a way to coordinate program completion.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Google Assistant IFTTT Recipes: 7 Useful Automations
- OLED TVs in Dark Rooms: Why They Look Better and When They Don’t
- Philips DST7060/20 7000 Series Iron: Specs & Fit
- Coal Fueled Jet Aircraft: Could It Work?675 L
- Remote Desktop on Raspberry Pi: How to Enable It
- Word Select Last Line in Paragraph?