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

9

73
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"

Lori answered 27/6, 2014 at 10:2 Comment(2)
Or also just wmic service get PathName. Works on command prompt too.Haematoblast
Shouldn't this be wmic service 'My Service' get pathname?Coincide
N
98

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

Get-CimInstance can also be used to achieve the same, see here for the difference between CIM and WMI.

Get-CimInstance win32_service | ?{$_.Name -like '*sql*'} | select Name, DisplayName, @{Name="Path"; Expression={$_.PathName.split('"')[1]}} | Format-List
Neomineomycin answered 27/6, 2014 at 10:40 Comment(7)
Thanks, but with the "PathName", I don't get the full path instead I get something like C:\Program Files (x86)\Microsoft SQL S... E:\apps\MySQL\MySQL Server 5.0\bin\mys... after certain number of characters the path truncates, is there any way to get the full path?Lori
Looks like something was missed off your comment, I'm going to guess that you have displayed it to the screen and the path is being trunctated. Try piping it through Format-List to see the full path.Neomineomycin
Thanks, I have tried Format-List and it works well. Can I truncate part of the path after .exe? for instance "C:\Program Files (x86)\Microsoft SQL Server\MSSQL.3\MSSQL\Binn\sqlservr.exe" -sSQLEXPRESS I don't need the part of path after .exe, I need something like this "C:\Program Files (x86)\Microsoft SQL Server\MSSQL.3\MSSQL\Binn\sqlservr.exe" Removing the quotes should be even betterLori
I've added an example for using calculated properties which should help you.Neomineomycin
Thanks for helping me out, the PathName.split cmdlet is what I was looking for, but it would have been better and more flexible if it supported Regular expressions, splitting based on " character doesn't apply to paths that don't have the " character. I tried replacing the quotes character with space and . characters and it resulted in truncated paths. Anyhow, your response has helped me well, thanksLori
If $_.PathName.Split doesn't give you the flexibility, then you can indeed use regex: $_.PathName -replace which does support regex, see here: blogs.technet.com/b/heyscriptingguy/archive/2011/03/21/…Neomineomycin
When you see those ... indicating the output has been truncated, I always use Export-CSV -NoTypeInformation to get the results in an easy to copy from format.Bengt
C
16

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.

If you don't need to check against a regular expression you can also use the -Filter parameter:

Get-CimInstance -ClassName win32_service -Filter "Name like 'sql%'" | Select Name, DisplayName, State, PathName >> C:\temp\sqlservices.txt
Control answered 21/4, 2020 at 11:0 Comment(1)
Note on Get-CimInstance you can add -Filter or -Query rather than piping to a separate filter.Utilize
A
7

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

Asserted answered 3/1, 2017 at 22:37 Comment(2)
And a variant on yours but using -Filter instead of -Query. I expect it's very similar under the hood but may be a fraction less typing :-) Get-WmiObject win32_service -Filter 'Name like "%SQL%"'Lamentable
The proposed answer doesn't work for me. The problem is that where Name like "*SQL*" should be where Name like "%SQL%" (notice that % is the wildcard for SQL)Anna
G
2

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
Gumboil answered 21/2, 2020 at 21:51 Comment(0)
B
1

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
Broiler answered 30/12, 2020 at 22:46 Comment(0)
G
1

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
Gerrit answered 11/2, 2021 at 23:8 Comment(0)
E
0

In Powershell 7 this property has been added, thus you can also use Get-Service meanwhile:

Get-Service *sql* | Select-Object -Property Status, Name, DisplayName, BinaryPathName
Eyestalk answered 17/7, 2023 at 7:39 Comment(0)
C
0

for me in powershell

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.17763.3770

adding select path works and prints:

PS C:\> Get-Service -Name *sql* | Select Path

Path
----
"C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\Binn\sqlservr.exe" -sMSSQLSERVER
Cursed answered 26/9, 2023 at 14:31 Comment(0)
G
0

Powershell as Admin:

Get-WmiObject win32_service | ?{$_.Name -like '*'} | select name, PathName | sort -Property pathname

Result

Glia answered 28/6, 2024 at 10:9 Comment(1)
Welcome to StackOverflow. Please, edit and try for How to Answer, describe the effect of what you propose and explain why it helps to solve the problem. Find help with formatting your post here: stackoverflow.com/help/formatting . Consider taking the tour.Donohue

© 2022 - 2025 — McMap. All rights reserved.