How to sleep for five seconds in a batch file/cmd
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Windows batch files do not have a built-in sleep keyword like some Unix shells, but delaying execution is still easy. In modern Windows environments the cleanest answer is timeout /t 5, while older compatibility scenarios sometimes still rely on ping or PowerShell.
Use timeout in Modern Batch Files
For most current systems, timeout is the right answer.
/t 5 means five seconds. /nobreak prevents the delay from being skipped by a key press. Redirecting to nul suppresses the default countdown text when you want cleaner output.
If you actually want the user to see the countdown, remove the redirection.
Use ping for Older Compatibility
Before timeout became common, batch files often used ping as a rough delay mechanism.
Why 6 instead of 5? Because ping sends one request immediately, then waits between the remaining sends. It is a workaround, not a true sleep function, so the timing is approximate.
Another version uses the timeout per ping request:
That waits up to about five seconds for a single request to time out.
Use PowerShell When You Need Precision or Richer Scripting
If PowerShell is available and calling it is acceptable in your environment, you can use its direct sleep command.
This is clear and readable, though it starts another process and is usually heavier than timeout for simple batch scripts.
Know the Difference Between Blocking and Waiting for Input
Sometimes what looks like a sleep requirement is really a pause for user confirmation. In that case, pause is the right command, not timeout.
Use pause for user interaction. Use timeout or another sleep mechanism for automatic delays.
Build Delays into Real Script Flow Carefully
A batch delay should have a reason. Common cases include waiting for a service to start, spacing retries, or giving a child process time to create a file. Even then, a fixed delay is often weaker than actively checking the condition you care about.
For example, instead of always sleeping for five seconds, poll until the expected file appears.
That pattern is more reliable than guessing how long the operation will take.
Choose the Simplest Tool That Matches the Environment
If the script targets standard modern Windows machines, use timeout. If you are stuck with older shells, ping remains a fallback. If your script is already mixing PowerShell and batch, Start-Sleep may be the cleanest readable option.
Common Pitfalls
- Using
pausewhen the goal is an automatic time delay rather than user interaction. - Forgetting
/nobreakand allowing a key press to skip the wait unexpectedly. - Treating the
pingworkaround as exact timing instead of approximate delay. - Adding fixed sleeps when polling for the real condition would be more reliable.
- Calling PowerShell for every tiny delay in a script where
timeoutis already available.
Summary
- '
timeout /t 5 /nobreakis the usual best answer for batch scripts.' - '
pingworks as an older compatibility workaround.' - PowerShell offers
Start-Sleepwhen that environment is already part of the script. - Use
pauseonly for user input, not for timed waiting. - Prefer checking for the real condition over adding arbitrary fixed delays.

