Created
December 1, 2019 13:17
-
-
Save pldmgg/9c4b23979a1fadefbca175563d1b2378 to your computer and use it in GitHub Desktop.
RenamePSCustomObjectNoteProperty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Rename-NoteProperty { | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory=$True)] | |
[pscustomobject]$MyObject, | |
[Parameter(Mandatory=$True)] | |
[String]$ExistingPropertyName, | |
[Parameter(Mandatory=$True)] | |
[String]$NewPropertyName | |
) | |
$NewHT = @{} | |
$myObject.psobject.properties | foreach { | |
if ($_.Name -eq $ExistingPropertyName) { | |
$NewHT[$NewPropertyName] = $_.Value | |
} | |
else { | |
$NewHT[$_.Name] = $_.Value | |
} | |
} | |
# Output | |
[pscustomobject]$NewHT | |
} | |
$TestObject = [PSCustomObject]@{ | |
Name = 'Paul' | |
Language = 'Powershell' | |
State = 'PA' | |
} | |
$UpdatedTestObject = Rename-NoteProperty -MyObject $TestObject -ExistingPropertyName "State" -NewPropertyName "Province" | |
$UpdatedTestObject |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment