Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to collect each Service name and its Status in Windows?

I want to fetch all service_name and its status without using any 3rd party tool. So far SC command was good enough to fetch one of the values, something like

sc query | findstr SERVICE_NAME

but I also need STATUS for each SERVICE_NAME listed.

like image 313
AabinGunz Avatar asked Aug 29 '12 07:08

AabinGunz


People also ask

How can I get details of service with service name?

To find the service name and display name of each service on your system, type Get-Service . The service names appear in the Name column, and the display names appear in the DisplayName column.

How do I get a list of services in Windows?

The services in Windows can be listed using the Service Manager tool. To start the Service Manager GUI, press ⊞ Win keybutton to open the “Start” menu, type in services to search for the Service Manager and press Enter to launch it. The services can also be listed using the command-line prompt (CMD) or the PowerShell.

How do I list all services in Windows Server?

The simplest command for listing Windows services on PowerShell is Get-Service. It shows all services on your computer, along with their status and names.

How do you check the status of a service in PowerShell?

Use PowerShell to Check Service Status on a Remote Computer You can use the Get-Service cmdlet to get the status of services not only on the local but also on remote computers. To do this, use the –ComputerName parameter. You can use the NetBIOS, FQDN name, or an IP address as a computer name.


3 Answers

Here's a command that should do the job:

for /f "tokens=2" %s in ('sc query state^= all ^| find "SERVICE_NAME"') do
    @(for /f "tokens=4" %t in ('sc query %s ^| find "STATE     "') do @echo %s is %t)

How it works:

First sc query state= all | find "SERVICE_NAME" is run. This command is designed to give you the service names, one per line. The carets ^ (which I have removed here) are necessary in order to escape the special characters that you want to affect the sc command and not the for command itself.

Then the initial for /f parses the above output to remove the standard "SERVICE_NAME:" prefix from each line, giving you pure service names. At this point the output looks like this:

C:\>for /f "tokens=2" %s in ('sc query state^= all ^| find "SERVICE_NAME"') do @echo %s
AdobeFlashPlayerUpdateSvc
AeLookupSvc
ALG
AppIDSvc
Appinfo
AppMgmt
aspnet_state
AudioEndpointBuilder
AudioSrv

This output is then fed to the next for /f, which runs sc query servicename, finds the line with the state, and isolates the 4th "word" (the current state).

Finally, the name of each service is printed along with its state (at this point you can choose to do something different if you wish).

Important note: If you run this inside a batch file, the percent signs (e.g. at %s) need to be doubled.

like image 80
Jon Avatar answered Oct 16 '22 04:10

Jon


By the way

sc query | findstr SERVICE_NAME

will show only active (RUNNING) services, so there is no need to filter status (STATE).

Try SC with findstr syntax that supports multiple filtered items:

sc query state= all | findstr "SERVICE_NAME STATE"

or

sc query state= all | findstr "DISPLAY_NAME STATE"

or both

sc query state= all | findstr "SERVICE_NAME DISPLAY_NAME STATE"
like image 35
Petr Charousek Avatar answered Oct 16 '22 06:10

Petr Charousek


Try Get-Service

PowerShell has Get-Service. It has a little less detail than sc.exe.

Try SC.exe

sc.exe query state= all

(Note: sc query state=all will NOT work. You NEED the space sign after the equals sign. Otherwise you will get this weird error: [SC] EnumQueryServicesStatus:OpenService FAILED 1060: The specified service does not exist as an installed service.)

Source: https://ss64.com/nt/sc.html

like image 2
StackzOfZtuff Avatar answered Oct 16 '22 06:10

StackzOfZtuff