split - Process.CommandLine gives two different results in powershell -


this seems should straightforward cannot seem find way it.

code

$processname = "notepad.exe" $filter = "name '%"+$processname+"'" $result = get-wmiobject win32_process -filter $filter $res = $result.commandline $res 

now when run above code, 2 different output $res when have different file open in notepad. why give me 2 different results?

output

"notepad.exe" full_file_path      "c:\windows\system32\notepad.exe" full_file_path 

when split $res output space using following code

$r = $res.split("",2) $r1 = $r[0] $r2 = $r[1] 

i get

$r

"c:\windows\system32\notepad.exe" full_file_path 

$r1

"c:\windows\system32\notepad.exe" 

$r2

full_file_path 

now folder notepad located using split-path,

split-path $r1 

i get

"c:\windows\system32 

instead of

c:\windows\system32 

but when run

split-path "c:\windows\system32\notepad.exe" 

i c:\windows\system32. why split-path when passed variable argument give "c:\windows\system32

the problem variable, string, contains embedded quotes , throws off split-path e.g.:

$p = '"c:\temp"' $p split-path $p 

outputs:

"c:\temp" "c:\ 

try eliminate quotes:

$r1 = $r1.trim('"') split-path $r1 

this outputs:

c:\windows\system32 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -