Home > PowerShell > Function: New-Sleep – When You Need a Delay

Function: New-Sleep – When You Need a Delay


On a recent project, I needed some PowerShell scripts to wait for a few seconds just to ensure that some other processes were finished and I wasn’t issuing too many commands to some Exchange servers too quickly. I came up with this little function:

function New-Sleep {
		[parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Mandatory = $true, HelpMessage = "No time specified")]
	for ($i=1; $i -lt $s; $i++) {
	Write-Progress -Activity "Waiting $s seconds..." -PercentComplete (100/$s*$i) -CurrentOperation "$TimeLeft seconds left ($i elapsed)" -Status "Please wait"
	Start-Sleep -Seconds 1
	Write-Progress -Completed $true -Status "Please wait"
} # end function New-Sleep

Call the function like this:

New-Sleep -s 60

Where the value of $s is the number of seconds you want to sleep. The display tells you how long your sleeping for, how much time is left, and how much time has elapsed.



Download the function below.


I’ve never been one to really solicit donations for my work. My offerings are created because *I* need to solve a problem, and once I do, it makes sense to offer the results of my work to the public. I mean, let’s face it: I can’t be the only one with that particular issue, right? Quite often, to my surprise, I’m asked why I don’t have a “donate” button so people can donate a few bucks. I’ve never really put much thought into it. But those inquiries are coming more often now, so I’m yielding to them. If you’d like to donate, you can send a few bucks via PayPal at https://www.paypal.me/PatRichard. Money collected from that will go to the costs of my website (hosting and domain names), as well as to my home lab.


v1.0 New-Sleep.ps1

Categories: PowerShell Tags: ,
  1. December 20th, 2011 at 13:15 | #1

    I like it. It would be helpful if you could make the function available as a text download. Copying code from the site is a bit ugly. And I think the final Write-Progress needs a -Activity parameter.

  2. Pat Richard
    December 20th, 2011 at 13:31 | #2

    Download link added. Haven’t seen the need for the final -Activity switch.

    • December 20th, 2011 at 13:34 | #3

      Hmmm…when I ran it I was always prompted. You really don’t even need the line in PowerShell 2.0.

  3. December 20th, 2011 at 13:32 | #4

    This is simply too much fun! The more I toy with it the more I think you can do with it. First, I’d suggest using a parameter name that is meaningful, like -Sleep or -Seconds. Since it is the only parameter, you could still run New-Sleep -s 5. But using a meaningful parameter name makes your function easier to discover and understand.

    I also think you could parameterize some of the messages. Keep what you have now, but as defaults. This would be more flexible if I could run it with a message that say, “Please wait while user data is processed”. Or another time I might use this with PowerCLI and want to say, “Waiting for VMHost”.

    I know you wrote this to fill a specific need, but with just a bit more work you would have something that adds even more value. Thanks for sharing.

  4. Pat Richard
    December 20th, 2011 at 13:47 | #5

    Yeah, I have a function that is all based around displaying the Write-Progess feature. I’ll have a post about that in the future. I use that all the time.

    The last Write-Progess is only there to ensure that it clears the progress indicator from the screen when it’s done. I’ve seen some times where it didn’t.

    Glad you like it.

  5. August 22nd, 2013 at 20:07 | #6

    Great function. Thanks!

  1. No trackbacks yet.