Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Process Location Path using PowerShell

I am trying to get the location of currently running process on your computer using PowerShell.

Example

C:\Program Files (x86)\Mozilla Firefox
C:\Windows\sysWOW64\WindowsPowerShell\v1.0
C:\Program Files (x86)\Internet Explorer

When I run the command

$path = Get-Process | Select-Object Path
Split-Path $path

I get the following output, which I not what I want. Why does it add @{Path=?

@{Path=C:\Program Files (x86)\Mozilla Firefox
@{Path=C:\Windows\sysWOW64\WindowsPowerShell\v1.0
@{Path=C:\Program Files (x86)\Internet Explorer

When I run Split-Path as follows, it gives me the correct output C:\Windows\sysWOW64\WindowsPowerShell\v1.0.

$pshpath = "C:\Windows\sysWOW64\WindowsPowerShell\v1.0\powershell.exe"
Split-Path $pshpath
like image 515
Ishan Avatar asked Jul 13 '15 02:07

Ishan


2 Answers

$path = Get-Process | Select-Object Path

returns an array of objects. Each object in the array will have the property 'Path' along with an optional value.

The 'path' parameter of split-path takes 'string' arguments so when you run Split-Path $path

i guess each object is being converted to type string so you get the hashtable format output.

split-path can accept path values from pipeline by property name so you can do:

 $path | Split-path

if you just want the path perhaps you could try:

Get-Process | Select-Object -ExpandProperty Path
like image 101
Kiran Avatar answered Oct 07 '22 02:10

Kiran


To get a list of all paths just use:

ps | % {$_.Path}

or full syntax:

Get-Process | ForEach-Object {$_.Path}

when using:

$path = Get-Process | Select-Object Path

lets look at what $path is:

$path | Get-Member

and you get:

   TypeName: Selected.System.Diagnostics.Process

Name        MemberType   Definition
----        ----------   ----------
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
Path        NoteProperty System.String Path=C:\windows\system32\atieclxx.exe

so Path is not a String but a NoteProperty, I guess that's why you can't use Split-Path directly.

like image 26
Peter Hahndorf Avatar answered Oct 07 '22 03:10

Peter Hahndorf