Skip to content

Instantly share code, notes, and snippets.

@vexx32
Last active April 25, 2019 13:37
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 vexx32/b5a4c16d22978c663333241cb8ca792e to your computer and use it in GitHub Desktop.
Save vexx32/b5a4c16d22978c663333241cb8ca792e to your computer and use it in GitHub Desktop.
$Number = 11
$Property = [PSNoteProperty]::new("IsNumber", $true)
$Number.PSObject.Properties.Add($Property)
$Number # Gives back 11, as you'd expect
$Number | Format-List -Force # shows the hidden property
#Bonus: ScriptProperties can use $this to refer to the object they're attached to
$Number = 7
$AnotherNumber = 42
$Property = [PSScriptProperty]::new("IsEven", {$this % 2 -eq 0})
$Property2 = [PSScriptProperty]::new("Value", {$this})
$Number.PSObject.Properties.Add($Property)
$Number.PSObject.Properties.Add($Property2)
$AnotherNumber.PSObject.Properties.Add($Property)
$AnotherNumber.PSObject.Properties.Add($Property2)
$Number, $AnotherNumber | Format-Table -Force
# Add-Member example
$Object = @{"Steve", 12}
$Object |
Add-Member -Name "Label" -Value "Hashtable" -MemberType NoteProperty -PassThru |
Format-Table -Force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment