PowerShell Start-Sleep

PowerShell’s Start-Sleep Cmdlet

Most scripting languages have the ability to pause, in the case of PowerShell look for a built-in cmdlet called Start-Sleep.  However, I ask myself, ‘Do I really need to pause a script?’  The answer often comes back, ‘Windows services have built-in controls to wait, thus I rarely need the extra and explicit PowerShell Start-Sleep’.

Topics for PowerShell Start-Sleep

 ♣

Example 1: PowerShell Start-Sleep

Here is a simple example where a PowerShell script pauses between starting and stopping a Windows service.

# Start-Sleep Example
Stop-Service Spooler -force
Start-Sleep -s 10
Start-Service Spooler
Get-Service Spooler

Note 1:  I am aware that we could simply use:
Restart-Service Spooler -force

Note 2: Observe how the timing parameter (-s) proceeds its numeric value (10).

This example does highlight that Start-Sleep may be a waste of time, and may make your script unnecessarily complex.  However, if we purely wanted to test Start-Sleep then this example would illustrate the simplicity of the syntax. 

Example 2: Start-Sleep with a PowerShell Job

Here is a more realistic example where Start-Sleep enables a script to wait while a ‘Job’ completes.

$job = Start-Job {$i=0; $c=0; while (1) {
Write-Progress Activity "Step $i"; $i++; Start-Sleep -sec 1 }}
while ($job.State -eq ‘Running’ -And $c -lt 5) {
$c++;
$progress=$job.ChildJobs[0].progress;
$progress | %{$_.StatusDescription};
$progress.Clear(); Start-Sleep 1 }

Guy Recommends: SolarWinds Free Wake-On-LAN UtilitySolarwinds Wake-On-LAN

Encouraging computers to sleep when they’re not in use is a great idea – until you are away from your desk and need a file on that remote sleeping machine!

WOL also has business uses for example, rousing machines so that they can have update patches applied.  My real reason for recommending you download this free tool is because it’s so much fun sending those ‘Magic Packets’. Give WOL a try – it’s free.

Download your free copy of SolarWinds Wake-On-LAN

Further Research of PowerShell’s Start-Sleep

I’m hoping that the simple example above will give you ideas.  To see what’s possible it’s well worth examining Start-Sleep’s properties.

Start-Sleep Parameters

# Research PowerShell’s Start-Sleep’s Parameters
Clear-Host
Get-Help Start-Sleep -full

Note 3:  All this help files reveals is how we can control the length of the pause with -s (seconds) or maybe -m (milliseconds).

Start-Sleep Properties

# Research PowerShell’s Start-Sleep Properties
Start-Sleep | Get-Member

Note 4:  Start-Sleep does not have any properties! For once PowerShell’s Get-Member cannot return any properties or methods for this cmdlet.

Start-Sleep Alias – Sleep

If you prefer, just use plain ‘Sleep’; this works because PowerShell has a built-in alias called Sleep.

Another Sleep Example

In this instance I wanted to highlight how Import-Module worked, specifically to count the modules before and after the import.

Clear-Host
Get-Module ; "Before " +(Get-Module).count
Start-Sleep -s 10
Import-Module PSRemoteRegistry
Write-Host `n"– After import –" `n
Get-Module ; "After" +(Get-Module).count

Note 5: You would need to have installed a module called PSRemoteRegistry for this to work!

Summary of Windows PowerShell Start-Sleep Cmdlet

In terms of efficiency, the crucial question is do we need Start-Sleep at all?  Once you decide that you need to pause a script, then the syntax is straightforward.

If you like this page then please share it with your friends

 


See more PowerShell examples of process and service

PowerShell Home   • Get-Process   • Stop-Process   • PowerShell Start-Process   • Set-Service

Get-Service   • Start-Service   • Stop-Service   • Restart-Service   • Free WMI Monitor

PowerShell Start-Sleep   • Get-WmiObject win32_service   • Windows PowerShell

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.