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”:


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:


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:


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:


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:


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:


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:


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.



  1. dexterposh

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

  2. vernanderson

    I like this one a lot great work Mike!

  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!

    • 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.

  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.

  5. Dorian


    What is the difference between this approach and taskkill?

  6. Anbu

    this can be used when we need to add both Stopping and Stoppending status…


    Start-Job -ScriptBlock {Restart-Service -Name “ColdFusion 9 ODBC Server” -Force }

    #give it 5 seconds to stop
    Start-Sleep -Seconds 10

    $SERVICESTATE = (Get-Service | where{$_.Name -eq “ColdFusion 9 ODBC Server”}).Status
    if( $SERVICESTATE -eq “Stopping” -or $SERVICESTATE -eq “StopPending”)
    # still stopping so force process stop
    Stop-Process -Name “swsoc” -Force

    #give it 5 seconds to start before we try it again
    Start-Sleep -Seconds 5

    $SERVICESTATE = (Get-Service | where{$_.Name -eq “ColdFusion 9 ODBC Server”}).Status
    if( $SERVICESTATE -eq “Stopped” )
    Start-Service -Name “ColdFusion 9 ODBC Server” -Force

  7. S Dsouza

    this saved my day. Thank you

  8. rick s.

    used this function in a script used for SENS service mstsc login hangup issue on multiple win server 2k12 r2. didn’t seem to work on svr 2k19 os but i didn’t look at the error log to see if stop-process or stop-service is what failed.

    foreach ($svr in $ALL_SERVERS)
    If ($((Get-Service -ComputerName $svr -Name “SENS”).status) -eq “Running”){
    Write-Host -ForegroundColor Cyan “$svr SENS service is: $((Get-Service -ComputerName $svr -Name “SENS”).status)”
    Stop-Service -svrName $svr -svcName “SENS” -Force
    Elseif ($((Get-Service -ComputerName $svr -Name “SENS”).status) -eq “StopPending”){
    Invoke-Command -ComputerName $svr -ScriptBlock ${Function:\Stop-PendingService}
    Else {
    Write-Host -ForegroundColor Cyan “$svr SENS service is: $((Get-Service -ComputerName $svr -Name “SENS”).status)”

  9. Alan

    excelente. esto me ayudo mucho


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: