Last active
April 25, 2019 13:37
-
-
Save vexx32/b5a4c16d22978c663333241cb8ca792e to your computer and use it in GitHub Desktop.
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
$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