How to execute the physical path of a Windows service using the Get-Service command

I need to deduce the physical implementation of all Windows services on a set of servers that run on Win 2k8. Since the powershell version shipped with this OS is 2.0, I wanted to use the Get-service command instead of Get-WmiObject. I know that I can pull the physical path using the command below

$QueryApp = "Select * from Win32_Service Where Name='AxInstSV'" $Path = (Get-WmiObject -ComputerName MyServer -Query $QueryApp).PathName 

I do not want this command to extend the physical path, but I wanted to use the Get-Service command, which comes with PS version 2.0.

Any help would be greatly appreciated.

+7
source share
4 answers

Even with PowerShell 3, I see no way to get it using Get-Service.

This 1-liner will give you the path name, although a little less than the preferred filter on the left:

 gwmi win32_service|?{$_.name -eq "AxInstSV"}|select pathname 

Or, if you only need the string itself:

 (gwmi win32_service|?{$_.name -eq "AxInstSV"}).pathname 
+10
source

I wanted to do something similar, but based on finding / matching the path of the process running under the service, so I used the classic WMI Query syntax and then passed the results through the format table:

 $pathWildSearch = "orton"; gwmi -Query "select * from win32_service where pathname like '%$pathWildSearch%' and state='Running'" | Format-Table -Property Name, State, PathName -AutoSize -Wrap 

You can turn this into a single liner by skipping the definition and passing $ pathWildSearch, or you can just go back to the gwmi instruction to continue working after the decimal point.

+2
source

Perhaps a little less detail,

 wmic service where "name='AxInstSV'" get PathName 

This should also work on the command line, not just the command line.


Or, if you have a process name that you could do:

 wmic process where "name='AxInstSV.exe'" get ExecutablePath 

To read the process path, you will need permission, so basically I got lucky with the service name.

0
source

I could never do this using the Get-Service command, but if your service starts as its own process, you can use the Get-Process command to do this through the following code:

 (Get-Process -Name AxInstSV).path 

Source: https://blogs.technet.microsoft.com/heyscriptingguy/2014/09/15/powertip-use-powershell-to-find-path-for-processes/

0
source

All Articles