1

It´s as simple as that Split-Path is removing the last '"' from every Pathname you query form any service:

$service = get-wmiobject -query 'select * from win32_service where name="SQLBrowser"';
Write-output $service.pathname 
Write-output $service.pathname | Split-Path

enter image description here

I tried with a few services and it's always the same.

Do you think this is a PowerShell bug we need to flag to Microsoft?

Is there any workaround?

EDIT: Thank you @mklement0 for the reply and the workaround.

It turns out this was indeed a Microsotf PowerShell bug

Francesco Mantovani
  • 10,216
  • 13
  • 73
  • 113

1 Answers1

3

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.

mklement0
  • 382,024
  • 64
  • 607
  • 775