PowerShell Function to Stop a Windows Service with a Status of Stopping

If you’re reading this blog article, you’ve probably tried to stop a Windows Service and it’s hung or stuck with a status of “Stopping”:

pendingservice1

An average Windows Admin would say “The server is going to have to be rebooted to clear up this problem”. This problem can be resolved with PowerShell though without the need for a restart.

The machine I’m testing these examples on just happens to be running PowerShell version 2 so I’ll use the v2 syntax to see if the Get-Service cmdlet has any properties that may help. I want to see values with the property names so I’ll pipe the results to Format-List * -Force as shown in the following example:

pendingservice2

There doesn’t appear to be any properties that can help so I’ll pipe Get-Service to Get-Member to make sure there aren’t any methods that could help:

pendingservice3

I don’t see anything in those results either that could help, but maybe we can just pipe the service with the issue to the Stop-Process cmdlet and use the Force parameter to stop it:

pendingservice4

No such luck. It’s looking like that restart may not be such a bad idea, but let’s give WMI a try before we resort to that option. We’re prototyping at this point so we’re not going to worry about filtering to the left:

pendingservice5a

The “ProcessId” property looks promising.

I’m sure most of you have either read about or seen examples on the Internet where someone tries to pipe Get-Service to Stop-Process and maybe they’re lucky enough to stop a process or two because those couple of service names just happen to match up to the process names, but generally speaking, service names and process names don’t match up.

We’re going to use a modified version of that same sort of example here, but we’re going to use the value of a service’s “ProcessId” which we’ll retrieve from WMI and feed that information to Stop-Process. While we’re at it, we’ll just write a simple function to accomplish this task and apply all of those best practices such as filtering to the left:

Our Stop-PendingService function will stop any service that has a state of “Stop Pending” on the local computer:

pendingservice6

Instead of writing a bunch of code in this function to connect to a remote machine, I’ll just leverage the power of PowerShell remoting via the Invoke-Command cmdlet and have it run this function that only exists on my local machine against the remote machine that has the service “Stopping” issue:

pendingservice7

The Stop-PendingService function can be downloaded from the TechNet Script Repository.

If you found this interesting and want to hear more of my PowerShell ramblings, I’m speaking on “Using CIM Cmdlets and CIM Sessions” at the Philadelphia PowerShell User Group meeting on Thursday, September 5th at 6pm EDT. Sign up here to attend in person or virtually via Microsoft Lync.

µ

5 Comments

  1. dexterposh

    Awesome stuff….waiting for more …
    Registered for Philly POSH UG meet.

    Reply
  2. vernanderson

    I like this one a lot great work Mike!

    Reply
  3. matthew.houston@camden.nsw.gov.au

    I know this is a bit old but just a quick note, this obviously wont work if the service that is stuck is the WMI service (winmgmt), which is the position I’m in right now!

    Reply
    • maxflipz

      In task manager, Services tab find WinMGMT then right click and choose GO TO Process. End tree that and you can now start WMI.

      Reply
  4. Dmitry

    I get an unexpected error: Cannot find process with the process identifier 2060..Exception.Message. Also I have process with state “Start pending” and processid equal 0.

    Reply

Leave a Reply

%d bloggers like this: