Igor
Igor

Reputation: 3

How to replace a symbol inside of every element's property of powershell array?

Good day. I imported a csv file into a variable

$header = "property1","property2"
$dt = Import-Csv $tempfile  -delimiter "," -header $header

and now want to replace one of the letters in each cell. I understand how to make the change explicitly specifying each property

$dt | % {
$_.'property1' = $_.'property1'.replace("'","")
$_.'property2' = $_.'property2'.replace("'","")
}

, but can't figure out how to do it without explicitly specifying the properties of the elements of the array. This example uses just two properties, but in a real script there are about 30..

Upvotes: 0

Views: 43

Answers (1)

Chris Dent
Chris Dent

Reputation: 4240

You can create a loop to run through the properties on the object. That allows you to access an modify the value under the heading.

Import-Csv $tempfile -Header $header | ForEach-Object {
    foreach ($property in $_.PSObject.Properties) {
        $property.Value = $property.Value.Replace("'", "")
    }
    # Output the modified line
    $_
}

Upvotes: 1

Related Questions