EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Sleep
Secondary Sidebar
PowerShell Tutorial
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • 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 Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • 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 Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • 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 do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • 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

PowerShell Sleep

By Priya PedamkarPriya Pedamkar

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:

The following is the syntax of the Start-Sleep cmdlet:

Name:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Start-Sleep

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,408 ratings)

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
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • 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

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

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

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more