Running Start-Sleep inside a continuous loop to check if a service is online will drain your CPU and eventually lock up your deployment. Relying on time-based delays for event-driven tasks is a critical mistake in automation. You need object-based waiting mechanisms and strict timeout parameters to build resilient scripts.

  • Fixed time delay: Start-Sleep -Seconds 10
  • Wait for process to close: Wait-Process -Name process_name
  • Wait for user input: Read-Host
  • Best for UI feedback: Write-Progress
  • Required for -Duration parameter: PowerShell 7 or higher

Start-Sleep: The Standard Way to Delay Execution

The most common way to halt script execution is Start-Sleep. Use this strictly for scenarios where you know exactly how much time an operation takes. The script freezes entirely, meaning no background tasks within the same session will process during this window.

Pausing for Exact Seconds

Start-Sleep -Seconds 5

You can also use the alias sleep 5 for quicker typing in the console. Do not use this method if the wait time depends on external factors like network speed.

Sub-Second Precision with -Milliseconds

Automation often requires delays shorter than a full second, especially when interacting with rapid API endpoints:

Start-Sleep -Milliseconds 500

Adding a half-second delay between API requests prevents rate-limiting blocks.

Using -Duration for Complex Timespans (PowerShell 7+)

PowerShell 7 introduced a much better way to handle complex wait times. The -Duration parameter accepts a standard timespan string:

Start-Sleep -Duration "01:30:00"

This tells the script to pause for exactly 1 hour and 30 minutes without manually calculating seconds.

Wait-Process and Wait-Event: Object-Based Pausing

Time-based delays fail when an application takes longer than expected to install or close. Object-based waiting solves this by monitoring the actual state of the system. The script only resumes when the specific condition is met.

Waiting for External Applications to Close

When your script triggers an uninstaller, it needs to wait for that process to finish before cleaning up files:

Wait-Process -Name "setup"

The console monitors the setup executable and instantly moves to the next line the millisecond that application terminates.

Adding -Timeout to Prevent Infinite Hangs

Relying on a process to close without a safety net is dangerous in production. If the target application crashes and leaves a ghost process, your script will wait forever. Always enforce a hard limit:

Wait-Process -Name "setup" -Timeout 120

If the setup process is still running after 120 seconds, PowerShell throws a non-terminating error. Catch this error to trigger an automatic rollback or alert the system administrator.

Read-Host: Pausing for User Interaction

Sometimes an automated task requires human validation before making destructive changes. You need the script to stop and wait for a manual keystroke.

Creating a Simple Press Enter to Continue Prompt

Read-Host "Press Enter to start the server reboot"

This is vital for step-by-step diagnostic scripts. It gives you time to review logs on screen before proceeding to the next phase. Read-Host is for interactive sessions only. In scheduled tasks or CI pipelines it will silently skip past the prompt, so running PowerShell scripts in unattended environments requires Start-Sleep or Wait-Process instead.

Hiding Input with -AsSecureString

Security protocols demand that you never display passwords in plain text on the terminal:

$apiKey = Read-Host "Enter your API Key" -AsSecureString

The characters turn into asterisks as you type. The script stores the input as an encrypted string object in memory.

Write-Progress: Adding a Countdown Timer for Better UX

A blinking cursor on a blank screen makes users think the script has frozen. Combine a sleep command with a progress bar:

for ($i = 10; $i -gt 0; $i--) {
    Write-Progress -Activity "Waiting for service" -Status "$i seconds left" -PercentComplete ($i * 10)
    Start-Sleep -Seconds 1
}

This creates a dynamic countdown at the top of the console. It shows exactly what the script is doing and how much longer it will take.

The Polling Anti-Pattern: When NOT to Use Start-Sleep

Many administrators write loops that sleep for five seconds, check if a file exists, and sleep again. This is the polling anti-pattern. It creates unnecessary CPU overhead and makes the code fragile.

Use Register-ObjectEvent for file system changes instead of building custom sleep loops. It is cleaner, faster, and consumes fewer system resources.

Legacy CMD Compatibility: The Pause Command

If you are migrating old batch files, the pause command still works but it operates differently:

cmd.exe /c pause

This launches a separate CMD instance just to display the classic "Press any key to continue..." message. It is inefficient and should be replaced with native PowerShell cmdlets in modern scripts. If you frequently run .exe files from PowerShell, the run EXE from CMD guide covers the argument and permissions differences worth knowing.

Which Method Should You Choose?

  • Start-Sleep: API rate limiting or fixed-time visual delays
  • Wait-Process: Software installations or external executables
  • Read-Host: Manual checkpoints or credential gathering
  • Write-Progress: Any wait time exceeding five seconds to prevent user confusion