Guy recommends :
Free SolarWinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



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 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: A Free Wake-On-LAN UtilitySolarwinds Wake-On-LAN

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

Wake-On-LAN really will save you that long walk to awaken a hibernating machine; however my reason for encouraging you to download this utility is just because it's so much fun sending those 'Magic Packets'.  As Wake-On-LAN (WOL) is free, see if I am right, and you get a kick from arousing those sleeping machines.  WOL also has business uses for example, wakening machines so that they can have their patches applied. 

Download your free copy of 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

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  • Get-WmiObject win32_service

Get-Service  • Start-Service  • Stop-Service  • Restart-Service  • Set-Service

PowerShell Start-Sleep   • 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.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, SolarWinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2012 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: