Reputation: 2673
I have the following sample code which replaces all comma values (,) with a period (.):
$foo = [PSCustomObject]@{
Num1 = "0.11"
Num2 = "0,12"
}
Write-Host "Type before:" $foo.GetType().FullName
$foo = $foo -replace(",",".")
Write-Host "Type after:" $foo.GetType().FullName
It produces the following output:
Type before: System.Management.Automation.PSCustomObject
Type after: System.String
I'd like to retain the PSCustomObject
type and not have it converted to a string. Any ideas on how to accomplish this aside from:
$foo.Num2.Replace(",",".")
I'd prefer not to get into listing out each property with a replace statement as my real code has MANY properties in this object.
Upvotes: 2
Views: 1715
Reputation: 200573
PowerShell comparison operators (including the replacement operator) implicitly convert the first operand to a type that matches the second operand. In your case that is transoforming the custom object to a string representation of itself.
To replace something in the properties of your object: enumerate the properties and do the replacement operation on the actual properties, not the object as a whole.
$foo.PSObject.Properties | ForEach-Object {
if ($_.Value -is [string]) {
$_.Value = $_.Value.Replace(',', '.')
}
}
Upvotes: 3