Skip to content

Instantly share code, notes, and snippets.

@pldmgg
Created December 1, 2019 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pldmgg/9c4b23979a1fadefbca175563d1b2378 to your computer and use it in GitHub Desktop.
Save pldmgg/9c4b23979a1fadefbca175563d1b2378 to your computer and use it in GitHub Desktop.
RenamePSCustomObjectNoteProperty
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