EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Sleep
 

PowerShell Sleep

Priya Pedamkar
Article byPriya Pedamkar

Updated March 4, 2023

PowerShell Sleep

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

PowerShell Sleep 1

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:

PowerShell Sleep 2

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:

PowerShell Sleep 3JPG

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:

Display status bar indicating progress

Display status bar indicating progress

PowerShell Sleep 6JPG

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:

user enters an input for the parameters

user enters an input for the parameters

PowerShell Sleep 9PG

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 –

  1. Windows PowerShell ISE
  2. PowerShell Send Mail
  3. PowerShell String Replace
  4. PowerShell Get-Service

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW