This article will demonstrate what the Start-Sleep command is, its syntax, and its execution in PowerShell. Furthermore, you will also check out some examples related to pausing script, assigning sleep time at execution, and viewing the manual of Start-Sleep in PowerShell. So let’s head towards this journey!
What is the Start-Sleep command in PowerShell?
The Start-Sleep cmdlet is used to suspend any session or activity for a specific time. It can be utilized for other tasks, such as pausing before operation repetition or waiting for some process to complete. Let’s look at the Start-Sleep command’s syntax.
Syntax of Start-Sleep command in PowerShell
Start-Sleep command comprises a simple syntax, offering you to specify the milliseconds or seconds as the specific time for sleep. Here is the syntax of the Start-Sleep command in PowerShell:
The Option is the parameter which can be “seconds or “milliseconds”:
-Seconds: This parameter specifies how long the activity or script should be suspended “in seconds.” It is also used as “-s” with the type “double.” The default value for this parameter is none. This option accepts pipeline input but does not accept wildcard characters.
For seconds, the Start-Sleep command is executed as:
Or
-MilliSeconds: This parameter specifies how long the activity or script should be suspended “in milliseconds.” It is also used as “-m” the type “int32”. This parameter’s default value is none. Pipeline input is accepted but not the wild cards.
For suspending activity for some milliseconds, you will specify the time in the following command:
or
Time: In the case of “-MilliSeconds,” you must specify the Time parameter in the “Int32” type. For “-Seconds,” take this option in type “Double.”
Let’s check out some PowerShell examples of the Start-Sleep command.
Start-Sleep command execution in PowerShell
Follow up this scenario: You have some background process to execute and pause the script. The execution of the background process will only take 15 seconds. With this, you have to be assured that your script does not run before the external event completes its execution. In this situation, what you have to do is utilize the Start-Sleep command with the 15 seconds as a “time parameter.”
The execution of this command will pause all of your PowerShell activities for 15 seconds.
You can specify the seconds according to your requirement. For instance, we will pause the PowerShell activities for “1.5” seconds in the below-given command:
In other circumstances, a more accurate time to suspend or sleep the execution is required. To specify the time in milliseconds, you can use the “-m” or the “-MilliSeconds” option. We will turn the PowerShell activities to sleep for ten milliseconds by writing out this command:
The same functionality can also be performed in the following way:
Specify the Sleep time at Run time in PowerShell
In PowerShell, you can also specify the seconds for your activity suspension. For this, execute the “Start-Sleep” command without any parameter.
After executing the above-given command, PowerShell will ask you for the number of seconds. Enter the suspension time as per your requirement.
Pausing the script in PowerShell
The Start-Sleep command is used in various situations, but one of the most common is a “loop,” when you are waiting for feedback or looking for a process state. This cmdlet works well with While and For loops. The duration of sleep can be set in seconds or milliseconds. You can pause your PowerShell script for 5 seconds by executing the below-given script:
while($value -ne 10)
{
$value++
Write-Host $value
if($value%5 -eq 0)
{
Start-Sleep -s 5
}
}
Execute your PowerShell script:
Want to pause your script with some specific milliseconds? Add your required milliseconds in the parameter. We have added “100” milliseconds suspension time in the following way:
while($value -ne 10)
{
$value++
Write-Host $value
if($value%5 -eq 0)
{
Start-Sleep -m 100
}
}
Execute this PowerShell script:
View the PowerShell Start-Sleep manual
To know more about the parameters, inputs, outputs, and aliases of the Start-Sleep command, write out the below-given command in your PowerShell:
Conclusion
In some situations, you are required to pause your PowerShell script for a few seconds. In PowerShell, you can utilize the Start-Sleep cmdlet to put an activity or a script to sleep for a particular amount of time. We have provided you with details about using the Start-Sleep command in PowerShell in this article. This information includes its parameters, values, and data types. We have also shown you some examples of the Start-Sleep cmdlet, which lets you sleep your PowerShell according to the specified time.