Another way of getting the path is by doing something like this:
(Get-Process -Name firefox).path
But, since one process can appear multiple times (I'm looking at you, chrome.exe
), you'd get the same path repeated as many times as the process appears in the process list. IMO, a better way of getting the path is by process id (1234 as an example):
(Get-Process -Id 1234).path
Or, if you're not sure what your process' id is, you could loop through all the running processes, and then check each of them individually, piping the results to a file, for later analysis. For example:
$processList = Get-Process #let's get all the processes at once
$newline = "`r`n"
$tabChar = "`t"
$separator = "--------"
$file = "C:\Users\Admin\Desktop\proc-loc.txt" # where do we want to pipe out the output
Write-Output "Processes $newLine $separator" > $file # out with the previous contents of the file, let's start anew
# let's loop through the list, and pick out the stuff we need
foreach($item in $processList) {
$itemObject = $item | Select-Object
$itemName = $itemObject.Name
$itemId = $itemObject.Id
$itemPath = (Get-Process -Id $itemId).path
Write-Output "$itemId $tabChar $itemName $tabChar $itemPath" >> $file
}
If you're interested in getting the running services as well, you could expand on the previous bit, with this:
$serviceList = Get-WmiObject win32_service | Where {$_.state -eq "Running"}
Write-Output "$newline $newline Services $newline $separator" >> $file
foreach($item in $serviceList) {
$itemName = $item.Name
$itemId = $item.ProcessId
$itemPath = $item.PathName
Write-Output "$itemId $tabChar $itemName $tabChar $itemPath" >> $file
}
One thing to note, though - this won't give you a path for each and every process currently running on your system. For example, SgrmBroker, smss, System
and some instances of svchost
won't have a path attached to them in your output file.
Get-Process -name java | % { $_.Path + " " + $_.Id}
– Mckeon