Igor Igor - 4 months ago 24
PowerShell Question

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..


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