PowerTip: Count a Range of Seconds with PowerShell

Summary: Use Windows PowerShell to count a range of seconds.

Hey, Scripting Guy! Question How can I use Windows PowerShell to count a range of seconds of increasing duration?

Hey, Scripting Guy! Answer Use the range operator to create a range of numbers, then pipe the results to the ForEach-Object cmdlet
          inside a script block call Start-Sleep:

1..5 | ForEach {Start-Sleep -Seconds $_ ; "$_ seconds"}

Comments (10)

  1. Fix it for me

    To fix this problem automatically, click the Fix button or link, click Run in the File Download dialog box and follow the steps in the Fix it wizard.

    Fix this problem
    Fix this problem
    Microsoft Fix it 50403

  2. This mechanism is used to stop the returned script running in an endless loop or otherwise perform slowly.

    In Internet Explorer 3.02, can this message box displayed on computers running Windows 95. Version 3.02 browser looking specifically to see if the thread script has pumped some windows messages in the last five seconds. You can not override this timeout for
    Internet Explorer 3.02. If you use an ActiveX control with long blocking operations should the ActiveX control used either an event mechanism and

  3. thank you Scripting Guy! Blog

    Learn about Windows PowerShell

  4. Because some scripts may take a long time to run, prompts the user to decide whether to continue working with slow scripts in Internet Explorer. Some tests and performance tests may use scripts that take a long time to run. Therefore you may want to increase
    the time it takes before the message box appears. In Internet Explorer script timeout value is changed on specific client computers by changing an entry in the registry.

  5. Boe Prox says:

    The example works as designed. Note Start-Sleep -Seconds $_ means that it is incrementing the seconds to match each iteration of the range. So it starts at 1 second and eventually goes to 5 seconds of sleeping by the end.

  6. Sometimes, a webpage contains script that takes an unusually long time to run. If you are scripting an ActiveX control on a webpage to transfer a very large file or run a large database query, this often causes a significantly long delay. Internet Explorer
    3.02 detects the long delay, and prompts the user with a dialog box that reads as follows:

    This page contains a script which is taking an unusually long time to finish. To end this script now, click Cancel.

    The equivalent message box for Internet Explor

  7. "Long running script" error messages are specific to Internet Explorer. These messages are generally intended for developers who are testing and building a website You should be able to get rid of them by disabling certain browser settings:
    Open Internet Explorer
    Click the ‘Tools’ menu, and then select ‘Internet Options’
    Click the ‘Advanced’ tab, select the ‘Disable script debugging (Internet Explorer)’ and ‘Disable script debugging (Other)’ check boxes
    Clear the ‘Display a notification about every script e

  8. Brinda says:

    How do I stop long running scripts?

  9. g-h says:

    can you given to me of some detail and some information, please. Thank you

  10. tomasz1 says:

    The script seems to run longer than 5 seconds and surely does not count every 1 second.

    Here some results from measure-command {1..5 | ForEach {Start-Sleep -Seconds $_ ; "$_ seconds"}}
    Days : 0
    Hours : 0
    Minutes : 0
    Seconds : 14
    Milliseconds : 988
    Ticks : 149885751
    TotalDays : 0.0001734788
    TotalHours : 0.0041634930
    TotalMinutes : 0.249809585
    TotalSeconds : 14.9885751
    TotalMilliseconds : 14988.5751

    Is PowerShell so slow ?

Skip to main content