EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PowerShell Sleep

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » PowerShell Tutorial » PowerShell Sleep

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.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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)

Popular Course in this category
Shell Scripting Training (4 Courses, 1 Project)4 Online Courses | 1 Hands-on Project | 18+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,157 ratings)
Course Price

View Course

Related Courses
All in One Data Science Bundle (360+ Courses, 50+ projects)Data Visualization Training (15 Courses, 5+ Projects)

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

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PowerShell Tutorial
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Rename Folder
    • PowerShell String Replace
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Get-ADUser
    • PowerShell Start-Process
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV 
    • PowerShell Export CSV
  • Basics
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Versions
    • How To Install PowerShell
    • How to Use PowerShell?
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Modules
    • Variable in PowerShell
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell vs PowerShell ISE
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
  • cmdlet
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • PowerShell Out-File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

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

Forgot Password?

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

Special Offer - Shell Scripting Course Learn More