The .PathName
property of the Win32_Service
instances returned by your Get-WmiObject
call:
sometimes contains values with embedded double quotes around the executable path
- Such embedded double quotes aren't part of the path and must be removed before further processing, such as via
Split-Path
.
may additionally contain arguments to pass to the executable, whether the latter is double-quoted or not.
Caveat: Some Win32_Service
instances return $null
in their .PathName
property.
To deal with both scenarios, use the following approach:
$service = get-wmiobject -query 'select * from win32_service where name="SQLBrowser"'
$serviceBinaryPath = if ($service.pathname -like '"*') {
($service.pathname -split '"')[1] # get path without quotes
} else {
(-split $service.pathname)[0] # get 1st token
}
# Assuming that $serviceBinaryPath is non-null / non-empty,
# it's safe to apply `Split-Path` to it now.
Do note that quite a few services use the generic svchost.exe
executable, so the .PathName
value doesn't necessarily reflect the service's specific binary - see this answer.
As an aside: Get-WmiObject
was deprecated in PowerShell v3 in favor of Get-CimInstance
- see this answer.