Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"

like image 538
Abilash A Avatar asked Jun 27 '14 10:06

Abilash A


People also ask

How do I find the path of a service?

If you right click any one of the Windows Services and then choose Properties, you would see the Service snap-in. In the Service snap-in, there is an item called "Path to Executable" that shows you the original location of your Windows Service's executable. Now I would like to get this path programmatically.

How do I get full path in PowerShell?

Use the Get-ChildItem cmdlet in PowerShell to get the full path of the file in the current directory. Get-ChildItem returns one or more items from the specified location and using the file FullName property, it gets the full path of the file.

How do I get a list of services in PowerShell?

To find the service name and display name of each service on your system, type Get-Service . The service names appear in the Name column, and the display names appear in the DisplayName column. When you sort in ascending order by status value, Stopped services appear before Running services.


1 Answers

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 
like image 73
David Martin Avatar answered Sep 21 '22 06:09

David Martin