To Get Running Service path on Windows

Awais Afzal Kamboh
2 min readAug 1, 2024

--

Powershell Script to retrieve and the display the executable path of a Windows Service name “LMS”

$serviceName = “LMS”

Set a variable with service name whom path we want to find

$service = Get-WmiObject -Query “SELECT * FROM Win32_Service WHERE Name = ‘$serviceName’”

Using Get-WmiObject to interact with Windows Management Insturmentation objects like hardware, software, network configuration and services [1]. Here we are querying to get information about the service. This will filter the services by the “Name” property to find the specific serverice “LMS”

if ($service -ne $null) {
$service.PathName
} else {
Write-Host “Service ‘$serviceName’ not found.”
}

It will check the service variable is not null, meant the service found it will output the “PathName” property, which contains the path to the service’s executable file. if the service is not found it print the message indicating that the service was not found.

Query

# Specify the service name
$serviceName = “LMS”

# Query the Win32_Service class to get information about the service
$service = Get-WmiObject -Query “SELECT * FROM Win32_Service WHERE Name = ‘$serviceName’”

# Check if the service was found
if ($service -ne $null) {
# Output the PathName property, which contains the executable path of the service
$service.PathName
} else {
Write-Host “Service ‘$serviceName’ not found.”
}

References

  1. https://ss64.com/ps/get-wmiobject.html

--

--