Search code examples
stringpowershellpipeline

Get Value from Power Shell Pipeline


I have data coming from another command output

testCommand will print something like this

a: 1
b: test
c: an3

I want to grep value of the specific property testCommand | findstr 'a', which prints a: 1.

But I want to extract the value 1. Couldn't figure out the way! If it doesn't exist print default value default


Solution

  • If you replace the : in the output with =, you can pipe it to ConvertFrom-StringData and get a nice hashtable instead:

    $values = testCommand
    $ValueTable = $values -replace ": ","=" |ConvertFrom-StringData
    $ValueTable["a"] # this will return the value "1"