Skip to content

Instantly share code, notes, and snippets.

@tackme31
Last active February 24, 2020 05:38
Show Gist options
  • Save tackme31/ef7e1a961eae6c8732a5d0006b14bbb0 to your computer and use it in GitHub Desktop.
Save tackme31/ef7e1a961eae6c8732a5d0006b14bbb0 to your computer and use it in GitHub Desktop.
Rename a property name of an object in PowerShell
$obj = [PSCustomObject]@{
Foo = "foo value"
Bar = "bar value"
Baz = "baz value"
}
$hogeProp = @{
Name = "Hoge"
Expression = {
return $_.Foo
}
}
$newObj = $obj | select *,hogeProp -ExcludeProperty Foo
# or select *,@{N="Hoge";E={$_.Foo}} -ExcludeProperty Foo
Write-Output $newObj
# Bar Baz Hoge
# --- --- ----
# bar value baz value foo value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment