Powershell Sleep (Start-Sleep) Cmdlet is a very useful scripting tool. There may be instances when you want to delay your script for some time before you proceed to the next line (see my second example). This is where you need Powershell Sleep.
In this short guide, you will learn the syntax, parameters and examples of Powershell Sleep (Start-Sleep) Cmdlet.
Powershell Sleep: Syntax of Start-Sleep Cmdlet
Here is the syntax of Start-Sleep Cmdlet:
Start-Sleep -Milliseconds <Int32>
Start-Sleep -Seconds <Int32>
This cmdlet has just two parameters: -Milliseconds – use it to specify the time (Int32) in milliseconds to suspend the activity of your script before it proceeds. You can also use the -Seconds parameter to specify the time in seconds.
Powershell Sleep Examples
This section provides some examples of how to use Powershell Sleep (Start-Sleep) Cmdlet.
Powershell Sleep Example 1
To delay your script for 30000 Milliseconds (30 seconds), use any of the commands below:
Start-Sleep -Milliseconds 30000
Start-Sleep -m 30000
When you run this command in PowerShell, the cursor will not return until after 30 seconds. See the image below. Notice that the cursor is not blinking. When the “sleep” completes, the command prompt will return. See the second image below.
Powershell Sleep Example 2
I used the method in this example in a script I wrote some years ago. The script in question enabled office 365 mailbox when an on-site Active Directory user is created. This was a fully automated process!
The process of enabling an office 365 mailbox involves a series of commands. The part of the script I used start-sleep is after I ran the commands below:
These two commands prepared the user’s account before the user’s office 365 mailbox is enabled. The above command needs some time to prepare the user’s account before the next command is executed.
To give the commands time to complete and the user account get ready, I added the command below before the next one:
Start-Sleep -Seconds 180
This forced the script to delay 180 seconds (3 minutes) before the next line of command ran. The portion of the script with the sleep command is shown below:
Once the delay completed, the script ran the command below:
I hope this short guide have simplified how to use Start-Sleep cmdlet for you. As I mentioned in my second example, this may be a very simple command but sometimes this simple knowledge can literally make you a star!
If you have any question or comment use the “Leave a Reply” form found at the end of this page. Alternatively, you could share your experience with Start-Sleep command.
Other Helpful Guides
- 18 Most Useful Powershell Commands for Windows Admins
- PowerShell Get-Command: Syntax, Applications, Examples