How can I extract "Path to executable" of all services with PowerShell

PowershellServicePowershell 2.0Powershell 3.0

Powershell Problem Overview


Get-Service *sql* | sort DisplayName | out-file c:/servicelist.txt

I have a one line PowerShell script to extract list of all services running on my local machine, now, in addition to displaying "Status", "Name" and "DisplayName" I also want to display "Path to executable"

Powershell Solutions


Solution 1 - Powershell

I think you'll need to resort to WMI:

Get-WmiObject win32_service | ?{$_.Name -like '*sql*'} | select Name, DisplayName, State, PathName

Update If you want to perform some manipulation on the selected data, you can use calculated properties as described here.

For example if you just wanted the text within quotes for the Pathname, you could split on double quotes and take the array item 1:

Get-WmiObject win32_service | ?{$_.Name -like '*sql*'} | select Name, DisplayName, @{Name="Path"; Expression={$_.PathName.split('"')[1]}} | Format-List

Solution 2 - Powershell

A variant on the WMI Query that may be faster (I just had to do this for an SCCM Client)

$SQLService=(get-wmiobject -Query 'Select * from win32_service where Name like "*SQL*"') | Select-object Name, DisplayName, State, Pathname

The other trick is to trap for the multiple SQL results if you want the path names without the Double Quotes (so you can action upon them)

$SQLService | Select-Object Name, DisplayName, State, @{Name='PathName';Expression=$_.Pathname.replace('"','')}

The big advantage to using -query in the get-wmiobject (or get-ciminstance) is the speed of processing. The older example gets a full list and then filters, whilst the latter grabs a very direct list.

Just adding in two cents :)

Cheers all! Sean The Energized Tech

Solution 3 - Powershell

Since Get-WmiObject have been deprecated in PowerShell Core, you can use

Get-CimInstance -ClassName win32_service | ?{$_.Name -match '^sql'} | Select Name, DisplayName, State, PathName >> C:\temp\sqlservices.txt

instead.

Solution 4 - Powershell

You can also use the Regular Expression pattern and dump the result to file.

Get-WmiObject win32_service | ?{$_.Name -match '^sql'} | select Name, DisplayName, State, PathName >> C:\temp\sqlservices.txt

Solution 5 - Powershell

I'm not comfortable with the accepted answer's use of Expression={$_.PathName.split('"')[1]}} because it doesn't handle the variants of quotes, spaces, and args that I see in the data.

Here's a clunky method that does.

function PathFromServicePathName($pathName) {
  # input can have quotes, spaces, and args like any of these:
  #   C:\WINDOWS\system32\lsass.exe
  #   "C:\Program Files\Realtek\Audio\HDA\RtkAudioService64.exe"
  #   C:\WINDOWS\system32\svchost.exe -k netsvcs -p
  #   "C:\Program Files\Websense\Websense Endpoint\wepsvc.exe" -k ss

  # if it starts with quote, return what's between first and second quotes
  if ($pathName.StartsWith("`"")) {
    $pathName = $pathName.Substring(1)
    $index = $pathName.IndexOf("`"")
    if ($index -gt -1) {
      return $pathName.Substring(0, $index)
    }
    else {
      # this should never happen... but whatever, return something
      return $pathName
    }
  }
  
  # else if it contains spaces, return what's before the first space
  if ($pathName.Contains(" ")) {
    $index = $pathName.IndexOf(" ")
    return $pathName.Substring(0, $index)
  }
  
  # else it's a simple path
  return $pathName
}

Get-WmiObject win32_service | select Name, DisplayName, @{Name="Path"; Expression={PathFromServicePathName $_.PathName}} | Format-List

Solution 6 - Powershell

A variant with Format-List with full path, results in file :

Get-WmiObject win32_service | Format-Table -Wrap -AutoSize -Property State,Name,PathName | out-file C:\servicelist.txt

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAbilash AView Question on Stackoverflow
Solution 1 - PowershellDavid MartinView Answer on Stackoverflow
Solution 2 - Powershelluser7371585View Answer on Stackoverflow
Solution 3 - PowershellMatthias AuswögerView Answer on Stackoverflow
Solution 4 - PowershellAidar GatinView Answer on Stackoverflow
Solution 5 - PowershellNathan SchubkegelView Answer on Stackoverflow
Solution 6 - PowershellmdelpeixView Answer on Stackoverflow