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

前端 未结 4 1142
猫巷女王i
猫巷女王i 2020-12-24 02:08
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 m

4条回答
  •  遥遥无期
    2020-12-24 02:30

    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

提交回复
热议问题