Updated March 4, 2023
Introduction to PowerShell Sleep
There are scenarios which requires a script to pause or stop for some time before continuing the execution. This type of requirement is handled with the help of Start-Sleep cmdlet in PowerShell. Its alias is Sleep. This cmdlet is used to stop the execution for the specified number of seconds. This cmdlet is one of the simplest to understand and execute as it has only two parameters. There are a wide range of scenarios where this cmdlet can be used. However, we should be prudent with its use and should know the exact time frame that needs to be specified in the sleep cmdlet for our operation to succeed.
Syntax:
The following is the syntax of the Start-Sleep cmdlet:
Name:
Start-Sleep
Syntax:
Start-Sleep [-Seconds] <int> [<CommonParameters>]
Start-Sleep -Milliseconds <int> [<CommonParameters>]
Aliases:
Sleep
Parameters:
- -MilliSeconds: This denotes the time in milli seconds for which the script or module should be suspended. Its shortened as m. Its type is int. Its alias in ms. Default value for this parameter is none. It accepts pipeline input, but wild card characters are not accepted.
- -Seconds: This denotes the time in seconds for which the script or module should be suspended. Its shortened as s. Its type is int. Its alias in none. Default value for this parameter is none. It accepts pipeline input, but wild card characters are not accepted.
Example:
Code:
Start-Sleep -Seconds 100 or Start-Sleep -s 100
Output:
The above cmdlet will make the session suspended or paused for 100 seconds.
Code:
Start-Sleep -MilliSeconds 10000 or Start-Sleep -ms 10000
Output:
The above cmdlet will make the session suspended or paused for 1000 seconds.
Examples of PowerShell Sleep
Given below are the examples mentioned:
Example #1
Code:
[int]$starttime = (Get-Date).Second
Write-Host "Welcome to the demo of PowerShell sleep"
Write-Host "Stopping the clock for five seconds"
Start-Sleep -Seconds 5
Write-Host "Stopping the clock for 10 seconds using alias"
Start-Sleep -s 10
Write-Host "Example of sleep using milliseconds"
Start-Sleep -MilliSeconds 100
[int]$endtime = (Get-Date).Second
Write-Host "Total time elapsed in second is" $($endtime - $starttime)
Output:
Example #2
Code:
[int]$starttime = (Get-Date).Second
Write-Host "Welcome to the demo of PowerShell sleep in while loop"
$test=0
while($test -ne 10)
{
$test++
Write-Host $test
if($test %2 -eq 0)
{
Write-Host "Sleeping for two seconds"
Start-Sleep -Seconds 2
}
}
[int]$endtime = (Get-Date).Second
Write-Host "Total time elapsed in second with sleep is" $($endtime - $starttime)
Write-Host "performing the same opeartion without sleep"
[int]$starttime = (Get-Date).Second
$test=0
while($test -ne 10)
{
$test++
Write-Host $test
if($test %2 -eq 0)
{
Write-Host $test
}
}
[int]$endtime = (Get-Date).Second
Write-Host "Total time elapsed in second without sleep is" $($endtime - $starttime)
Output:
Example #3
Code:
[int]$starttime = (Get-Date).Second
$fatherage = Read-Host "Enter your fathers age"
Start-sleep -Seconds 5
$motherage = Read-Host "Enter your mothers age"
Start-Sleep -Seconds 5
$yourage = Read-Host "Enter your age"
Start-Sleep -Seconds 5
$sistersage = Read-Host "Enter your sisters age"
Start-Sleep -Seconds 5
$avg=([int]$fatherage + [int]$motherage + [int]$sistersage +[int]$yourage)/4
Write-Host "the average age of your family is" $avg
[int]$endtime = (Get-Date).Second
Write-Host "Total time elapsed in second without sleep is" $($endtime - $starttime)
Output:
Example #4
Display status bar indicating progress.
Code:
[int]$starttime = (Get-Date).Second
Write-Host "Welcome to the demo of start-sleep"
Function Start-Sleep($seconds)
{
$dt = (Get-Date).AddSeconds($seconds)
while($dt -gt (Get-Date))
{
$secondsLeft = $dt.Subtract((Get-Date)).TotalSeconds
$per = ($seconds - $secondsLeft) / $seconds * 100
Write-Progress -Activity "in progress" -Status "in progress..." -SecondsRemaining $secondsLeft -PercentComplete $per
[System.Threading.Thread]::Sleep(500)
}
Write-Progress -Activity "in progress" -Status "in progress..." -SecondsRemaining 0 -Completed
}
Write-Host "going to sleep for 1 second"
Start-Sleep 1
Write-Host "going to sleep for 2 second"
Start-Sleep 2
Write-Host "going to sleep for 3 second"
Start-Sleep 3
Write-Host "going to sleep for 4 second"
Start-Sleep 4
Write-Host "going to sleep for 5 second"
Start-Sleep 5
Write-Host "going to sleep for 6 second"
Start-Sleep 6
Write-Host "going to sleep for 7 second"
Start-Sleep 7
Write-Host "going to sleep for 8 second"
Start-Sleep 8
Write-Host "going to sleep for 9 second"
Start-Sleep 9
Write-Host "going to sleep for 10 second"
Start-Sleep 10
[int]$endtime = (Get-Date).Second
Write-Host "Total time elapsed in second without sleep is" $($endtime - $starttime)
Output:
Example #5
Wait till the user enters an input for the parameters.
Code:
[int]$starttime = (Get-Date).Second
Write-Host "Welcome to the demo of start-sleep"
Function Start-Sleep($seconds)
{
$dt = (Get-Date).AddSeconds($seconds)
while($dt -gt (Get-Date))
{
$secondsLeft = $dt.Subtract((Get-Date)).TotalSeconds
$per = ($seconds - $secondsLeft) / $seconds * 100
Write-Progress -Activity "in progress" -Status "in progress..." -SecondsRemaining $secondsLeft -PercentComplete $per
[System.Threading.Thread]::Sleep(500)
}
Write-Progress -Activity "in progress" -Status "in progress..." -SecondsRemaining 0 -Completed
}
$sname=Read-Host "Enter student name"
Start-Sleep 2
while($sname -eq $null -or $sname -eq "")
{
$sname=Read-Host "Enter student name"
Start-Sleep 2
}
$sage=Read-Host "Enter student age"
Start-Sleep 2
while($sage -eq $null -or $sage -eq "")
{
$sage=Read-Host "Enter student age"
Start-Sleep 2
}
$smarks1=Read-Host "Enter marks in history"
Start-Sleep 2
while($smarks1 -eq $null -or $smarks1 -eq "")
{
$smarks1=Read-Host "Enter marks in history"
Start-Sleep 2
}
$smarks2=Read-Host "Enter marks in bio"
Start-Sleep 2
while($smarks2 -eq $null -or $smarks2 -eq "")
{
$smarks2=Read-Host "Enter marks in bio"
Start-Sleep 2
}
$smarks3=Read-Host "Enter marks in phy"
Start-Sleep 2
while($smarks3 -eq $null -or $smarks3 -eq "")
{
$smarks3=Read-Host "Enter marks in phy"
Start-Sleep 2
}
Write-Host "Student details are as follows"
$avg=([int]$smarks1+[int]$smarks2+[int]$smarks3)/3
Write-Host "Student name is:" $sname "`t Students age is:" $sage "`t students avg is:"$avg
Output:
Conclusion
Thus, here we saw in detail about the start-sleep cmdlet. It explained from the parameters, their data types to their default values. It also show with examples on how to call the cmdlet and their parameters using the alias values. It demonstrated the use of Start-sleep cmdlet in many scenarios like waiting for a value in while loop, asking input to the user unless he supplies a value, etc.
Recommended Articles
This is a guide to PowerShell Sleep. Here we discuss the introduction to PowerShell Sleep along with examples respectively. You may also have a look at the following articles to learn more –