How to add Sleep to PowerShell Script


If you want to add a sleep (delay) to your PowerShell script, you need to make use of the Start-Sleep cmdlet,

Let's take a look at a few examples.


Example 1: One-Liner Sleep

    echo "Sleeping for 10 seconds"; Start-Sleep -Seconds 10; echo "Done!"

    Sleeping for 10 seconds
    Done!


Example 2: Sleep with Script

    $numbers = 1..10
    
    foreach ($num in $numbers) {
        # Sleep for 1 seconds
        Start-Sleep -Seconds 1
        Write-Host $num
    }
    
    ./powershell-sleep-script.ps1
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
PowerShell Sleep Script Example
-

Facing issues? Have Questions? Post them here! I am happy to answer!


Author Info:

Rakesh (He/Him) is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap