#PSTip Wait for a Service to reach a specified status

So, in your script, you need to wait for a service until it reaches a specified status and performs an action based on the new state. One way to achieve this, and an ineffective one, would be to poll the status using a while loop:

$svc = Get-Service W3SVC
while($svc.State -ne 'Stopped')
	Start-Sleep -Seconds 1

... do your thing here...

Instead, you could wait for the service to reach the specified status using one of its native methods:


This will wait infinitely for the service to reach the specified state, and script execution is halted until the service state changes. Waiting forever for the service to change its state may not be what we want to do, so instead we can use the second overload of the WaitForStatus method and specify an expiration time-out value.

# wait for 5 seconds

... the rest of the script ...
Filed in: Columns, Tips and Tricks Tags: , ,

11 Responses to "#PSTip Wait for a Service to reach a specified status"

  1. pamarths says:

    Hi, Small correction to above examples. The ineffective method has some typos. There is no parameter called “state” for the object $SVC. It should be changed to “Status”. And inside while loop, a extra statement should added, $svc.refresh() so that it gets the latest status of service each time it is entering while loop.

    • ShayLevy says:

      Thanks for the feedback @pamarths. I used the term ‘state’ to describe the current configuration of the service, not for referring to its ‘Status’ property. I agree it might be confusing.

  2. DVS says:

    What’s wrong with polling the status using a while loop, other than more lines of code? Checking out the WaitForStatus on MSDN is says “The WaitForStatus method waits approximately 250 milliseconds between each status check.” So to me is sounds like it’s just doing some looping too.

  3. Igor says:

    How do i make it wait more than 30 seconds?

    i run this method and it times out at 30 seconds. so the wiat for status never really runs.



  4. Chris K. says:

    Except in practice WaitForStatus doesn’t work on an object. You can try it for yourself. Once you create an object, that object is static and does not reflect status changes unless you use the Refresh method. I don’t know why they didn’t build Refresh into WaitForStatus but as it stands, it’s a worthless method for using on an object. The only way you can use WaitForStatus would be like:
    (Get-Service “service”).WaitForStatus(‘stopped’,’00:00:05′)

    • David Lant says:

      PowerShell 4.0: It does seem to work for objects to me.

      $svc = Get-Service w3svc
      Stop-Service w3svc
      # Still shows as Running
      #Returns instantly because the status has been refreshed
      # Now shows the Stopped status

      It seems like only the Status method doesn’t issue a Refresh, while the WaitForStatus does. Or am I missing something?

      • Adam Morrison says:

        I know this is kind of an old post, but I like using these methods more than a while-loop. No good reason other than it looks cleaner to me. But here is my two cents.

        I believe this is because one is a method and one is a property.

        If you do $svc | Get- Member you can see that one is a “Method” and one is a “Property”.

        I’m very much a script guy with dev buddies, but I believe that is the difference. Methods would be actions and return active data. A property is just the static value of an object.

        So you can you can do $svc.refresh(), which is a method, and then do $svc.status and that will update the property of the object.

    • Tracie says:

      Hey Chris,
      Your method work whereas the authors did not for me.
      As a brand new PowerShell user and a pretty week coder – could you explain a little more? Is it that you are “creating an object on the fly” with (Get-service “service”) rather than creating a static object?

Leave a Reply

Submit Comment

© 2018 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: