February 2019
Intermediate to advanced
626 pages
15h 51m
English
Add-Member allows new members to be added to existing objects.
Starting with an empty object, it is possible to add new properties:
PS> $empty = New-Object ObjectPS> $empty | Add-Member -Name New -Value 'Hello world' -MemberType NotePropertyPS> $emptyNew---Hello world
Add-Member may also add a ScriptProperty or a ScriptMethod. When writing script-based properties and methods, the reserved variable $this is used to refer to itself.
To add calculated properties, which are evaluated when viewed, use the following code:
PS> $empty = New-Object ObjectPS> $empty | Add-Member -Name New -Value 'Hello world' -MemberType NotePropertyPS> $empty | Add-Member -Name Calculated -Value { $this.New.Length } -MemberType ScriptProperty ...
Read now
Unlock full access