How can i replace strings in properties of PSOBJECTS in an array?
You do it for each object.
$thisIsTheArrayOfObjects | ForEach-Object {
$_.foo = $_.foo -replace "Old ID" , "New ID" # replace string in a property
$_ # output the object back onto the pipeline
}
Keep in mind that there are two gotchas with -replace
:
- It works with regex. Technically you must regex-escape the first parameter (
[regex]::escape("Old ID")
) if you want to do a literal replacement, unless "Old ID" does not contain any characters that are special in regex. If it comes from user input, it's always wise to escape it.
- It's case-insensitive.
To do case-sensitive, literal replacements, use the .NET-native String.Replace()
, which is also available:
$thisIsTheArrayOfObjects | ForEach-Object {
$_.foo = $_.foo.Replace("Old ID", "New ID") # replace string in a property
$_ # output the object back onto the pipeline
}
To apply an operation to every column in the CSV data, you have to create a nested loop. The Get-Member
cmdlet can help. The principle stays the same as above.
Import-Csv "input.csv" -PipelineVariable row | ForEach-Object {
$row | Get-Member -MemberType NoteProperty -PipelineVariable col | ForEach-Object {
$colname = $col.Name
$row.$colname = $row.$colname -replace "Old ID", "New ID"
$row
}
} # | Export-Csv "output.csv"
Note the use of pipeline variables to sidestep any $_
confusion inside the inner loop.