How to Get a Service's Display Name in Batch Script
When working with Windows services from the command line, you are often dealing with two different identifiers: the Service Name and the Display Name. The Service Name (e.g., wuauserv) is the short, unique, internal name used by the system. The Display Name (e.g., "Windows Update") is the longer, more user-friendly name you see in the services.msc console.
While you often need the short name to control a service, you may need the display name for creating human-readable reports or log files. This guide will teach you how to use the built-in SC.EXE (Service Control) utility to get the friendly display name for any service if you know its short name.
Service Name vs. Display Name: A Critical Distinction
It is essential to understand the difference between these two names.
| Identifier | Description | Example | Used For |
|---|---|---|---|
| Service Name | The short, programmatic name. It is unique and has no spaces. | wuauserv | Controlling the service (SC START, SC STOP, etc.) |
| Display Name | The long, human-readable name. It can contain spaces and special characters. | Windows Update | Displaying information in GUIs and reports. |
Most SC commands require you to use the Service Name. The GetDisplayName command is one of the few that works with it to retrieve the other.
The Core Command: SC GetDisplayName
The sc.exe utility is the primary tool for all service management. The GetDisplayName sub-command is designed for this specific task.
Syntax: SC GetDisplayName <ServiceName>
<ServiceName>: The short, internal service name (e.g.,Spooler).
Basic Example: Getting the Display Name for wuauserv
This script runs the command to find the friendly name for the wuauserv service.
@ECHO OFF
SET "ServiceName=wuauserv"
ECHO --- Getting the Display Name for '%ServiceName%' ---
ECHO.
SC GetDisplayName %ServiceName%
The output is formatted as a key-value pair, with some extra spacing.
--- Getting the Display Name for 'wuauserv' ---
[SC] GetServiceDisplayName SUCCESS
Name = Windows Update
How to Capture the Display Name in a Variable
To use the display name in your script, you need to parse the output of the SC command and capture the value. A FOR /F loop is the perfect tool for this.
@ECHO OFF
SET "ServiceName=Spooler"
SET "DisplayName="
ECHO --- Capturing Display Name for '%ServiceName%' ---
REM 'tokens=2*' splits the line by the first space.
REM 'delims=:' splits by the colon, so %%B gets the value.
FOR /F "skip=1 tokens=1,* delims=:" %%A IN (
'SC GetDisplayName "%ServiceName%"'
) DO (
SET "DisplayName=%%B"
)
REM The result has a leading space, so we need to remove it.
FOR /F "tokens=*" %%N IN ("%DisplayName%") DO SET "DisplayName=%%N"
ECHO.
ECHO The Display Name is: "%DisplayName%"
How the Command Works
The SC GetDisplayName command directly queries the Service Control Manager (SCM), which is the core Windows service responsible for managing all other services. It looks up the service by its short name in the registry (HKLM\SYSTEM\CurrentControlSet\Services\<ServiceName>) and retrieves the value of the DisplayName registry string.
Common Pitfalls and How to Solve Them
Problem: Using the Wrong Service Name
If you provide a service name that doesn't exist, the command will fail.
Example of error message:
[SC] GetServiceDisplayName FAILED 1060:
The specified service does not exist as an installed service.
Solution: Ensure you are using the correct internal service name. You can find it with SC QUERY or by looking at the service's properties in services.msc.
Problem: Parsing the Output is Tricky
The output Name = Value has spaces around the = (or : in some versions), which can make parsing with FOR /F a bit clumsy.
Solution: The two-step parsing shown in previous sections is the most robust method:
- First
FOR /F: Usedelims=:to split the key from the value. This will capture the value but with a leading space. - Second
FOR /F: A simpleFOR /F "tokens=*" %%N IN ("%VarWithSpace%") DO SET "Var=%%N"is a standard batch trick to remove leading and trailing whitespace from a variable.
Practical Example: A "Service Status Report" Script
This script gets a list of all services, and for each one, it retrieves and displays its current state alongside its more user-friendly Display Name.
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO --- Generating Service Status Report ---
ECHO This may take a moment...
ECHO.
REM 'tokens=2' gets the service name from the SC QUERY output.
FOR /F "tokens=2" %%S IN ('SC QUERY STATE= all ^| FIND "SERVICE_NAME"') DO (
SET "ServiceName=%%S"
REM --- Get the Display Name for the current service ---
SET "DisplayName="
FOR /F "skip=1 tokens=1,* delims=:" %%A IN ('SC GetDisplayName "!ServiceName!"') DO (
SET "DisplayName=%%B"
)
REM Clean up the leading space
FOR /F "tokens=*" %%N IN ("!DisplayName!") DO SET "DisplayName=%%N"
REM --- Get the Status for the current service ---
SET "Status="
FOR /F "tokens=3" %%T IN ('SC QUERY "!ServiceName!" ^| FIND "STATE"') DO (
SET "Status=%%T"
)
ECHO [!Status!] - !DisplayName! (!ServiceName!)
)
ENDLOCAL
Conclusion
The SC GetDisplayName command is the standard and most reliable way to translate a service's short, internal name into its user-friendly Display Name.
Key takeaways for using it in a script:
- You must provide the correct internal Service Name (e.g.,
wuauserv). - The output format is a
Name = Valuepair, which requires careful parsing with aFOR /Floop. - Remember to trim the leading space from the captured value for clean output.
While most service control commands use the short name, this command is essential for creating scripts that produce clear, human-readable reports and logs.