Get Process Location Path using PowerShell

42,197

Solution 1

$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

Solution 2

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.

Share:
42,197
Ishan
Author by

Ishan

Data Analyst with a development background currently looking for a job.

Updated on November 21, 2020

Comments

  • Ishan
    Ishan over 3 years

    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