≡ Menu

How to query service startup type using Powershell

In this post, I will show you how to query startup type (manual/automatic/disabled) of windows service on local or remote computer using Powershell and WMI.

Often people ask me why Get-Service is not returning Service startup type. That is Automatic/manual/disabled. I too don’t have a reason for that. But all I can say is, Get-Service is not tuned to give that information. The next question is how to query and what is the alternative.

The Win32_Service WMI class in Windows provides way to get this information. Most people neglect WMI and rely more on built-in cmdlets. While I don’t blame built-in cmdlets for anything, apart from limited functionality, I recommend using WMI or dotnet classes to get the information. They are fast, reliable, and the way is efficient.

Now let us move on to the original topic of how to query service startup type using Powershell and WMI. Since we decided to use WMI, use Get-WMIObject cmdlet to query the Win32_Service class.

Here is a small powershell script (Get-ServiceStartupType.ps1) which queries the startup type of given service.

[cmdletbinding()]            

param(            
 [string[]]$Service,            
 [switch]$Disabled,            
 [switch]$Automatic,            
 [switch]$Manual,            
 [string]$ComputerName = $env:ComputerName            
)            

foreach($Ser in $Service) {            
 try {            
  $Obj = Get-WmiObject -Class Win32_Service -Filter "Name='$Ser'" -ComputerName $ComputerName -ErrorAction Stop            
  $Obj | select Name, DisplayName, StartMode            
 } catch {            
  Write-Error " Failed to get the information. More details: $_"            
 }            
}

 

Examples:

  1. .\Get-ServiceStartupType.ps1 –Service Wsearch — queries the startup type of single service
  2. .\Get-ServiceStartupType.ps1 –Service Wsearch –ComputerName mypc1 — queries the startup type of single service on remote computer, mypc1.
  3. .\Get-ServiceStartupType.ps1 –ComputerName mypc1 – queries the startup type of all services on remote computer, mypc1.
  4. .\Get-ServiceStartupType.ps1 –Service Wsearch, wwansvc – queries the startup type of multiple services.

Output:

Comments on this entry are closed.

  • Sreeramulu July 21, 2014, 9:19 pm

    Thanks for the suggestion provided. I got my query answered here 🙂

  • Jim February 13, 2017, 3:43 am

    I’ve been trying various things to get the true startup type of a service but am unable. The startup type of the service is Automatic (Delayed Start). For some reason, I can’t seem to find the powershell code to get this value. The powershell returns the start type of auto, but doesn’t show the delayed start portion.
    If i use cmd, as the below, then I get the true value.
    ———- cmd code ———-
    for /f %x in (serverlist.txt) do @echo Getting from server %x && sc \\%x qc MyService | findstr /C:”START_TYPE”

    ———- results ———
    Getting from server server1
    START_TYPE : 2 AUTO_START (DELAYED)
    Getting from server server2
    START_TYPE : 2 AUTO_START (DELAYED)

    Is there anything in powershell that can give me the true start type like this?

    • Wintel Rocks February 25, 2017, 11:09 am

      Hi,

      You are correct. The new startup types like delayed start are not reachable through PowerShell/.Net. At the moment, writing wrappers around sc command seems only the way. I will update the blog if I come across any thing better.

  • Chris Abbott April 5, 2017, 8:04 pm

    This is good, but I’m looking to know about a single service, and if the value was returned as true/false or just “manual” “disabled” or “automatic” so I could prompt the script to decide on the next action. Any idea how I could do that?

    • Wintel Rocks April 9, 2017, 7:25 pm

      Chris, I didn’t get yoour requirement clearly. Do you have an example?