Powershell: Setting values of a filtered array
Posted
by Alan T
on Stack Overflow
See other posts from Stack Overflow
or by Alan T
Published on 2010-06-17T16:16:33Z
Indexed on
2010/06/17
16:23 UTC
Read the original article
Hit count: 498
Hi All
I want to read in a csv file, filter it based on the values of two of the fields and set the value of another field. Here's a simple example of what I'm trying to achieve:
c:\somefile.csv contents:
firstField,secondField,thirdField
1,2,"somevalue"
2,2,"avalue"
3,1,"somevalue"
#Import file into array
$csv = Import-Csv c:\somefile.csv
# Where secondField = 2 and thirdField = "someValue" set thirdField =
"anotherValue"
$csv | where {$_secondField -eq 2 -and $_.thirdField = "somevalue"} |
<set value of thirdField = "anotherValue">
How can I do this. As you can see, from the example about, I can read in and filter the array. But I don't know how to then set the value of the thirdField. I tried set-itemproperty but got the error: "The WriteObject and WriteError methods cannot be called after the pipeline has been closed".
Any advice on how to achive this would be appreciated.
Alan T
© Stack Overflow or respective owner