Updating Objects
suggest changeAdding properties
Add-Member adds a property to an existing object. On a PSObject, an extra value is a NoteProperty. $env:USERNAME is set on Windows. The name nem below is one computer's user name.
$object = New-Object -TypeName PSObject -Property @{
Name = $env:username
ID = 12
Address = $null
}
Add-Member -InputObject $object -Name "SomeNewProp" -Value "A value" -MemberType NoteProperty
# Returns
PS> $Object
Name ID Address SomeNewProp
---- -- ------- -----------
nem 12 A value
Select-Object returns a new object. A calculated property adds a value to that copy:
$newObject = $Object | Select-Object *, @{label='SomeOtherProp'; expression={'Another value'}}
# Returns
PS> $newObject
Name ID Address SomeNewProp SomeOtherProp
---- -- ------- ----------- -------------
nem 12 A value Another value
The command above can be shortened to this:
$newObject = $Object | Select *,@{l='SomeOtherProp';e={'Another value'}}
Removing properties
Select-Object -ExcludeProperty returns a new object without those properties. It does not change the original object:
$object = $newObject | Select-Object * -ExcludeProperty ID, Address
# Returns
PS> $object
Name SomeNewProp SomeOtherProp
---- ----------- -------------
nem A value Another value
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents