Powershell

Sleep command in PowerShell

In some situations, you are required to pause your PowerShell script for a few seconds. For instance, to verify if a server is back online. In PowerShell, the Start-Sleep cmdlet is used to handle this type of case. This PowerShell command (cmdlet) pauses the execution for the number of seconds specified. The Start-Sleep command requires two parameters which makes this cmdlet the most effortless command to use. This command can be utilized in various scenarios. However, you should be aware of the exact time frame for which you will pause PowerShell activity.

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:

Start-Sleep [Option] [Time]

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:

Start-Sleep -Seconds Time

Or

Start-Sleep -s Time

-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:

Start-Sleep -MilliSeconds Time

or

Start-Sleep -m Time

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.”

> Start-Sleep -s 15

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:

> Start-Sleep -Seconds 1.5

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:

> Start-Sleep -m 10

The same functionality can also be performed in the following way:

> Start-Sleep -MilliSeconds 10

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.

> Start-Sleep

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:

$value=0

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:

$value=0

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:

> Get-Help Start-Sleep -Full

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.

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.