Appending Extra Information to Primitive Data Types

by Aug 17, 2015

Maybe you’d like to tag a variable and provide some extra information. In PowerShell, use Add-Member and attach NoteProperties or ScriptProperties to it.

A NoteProperty contains some static information, whereas a ScriptProperty runs code when you retrieve the property.

Have a look how that would work with a simple string:

$a = "some text"

$a = $a | Add-Member -MemberType NoteProperty -Name Origin -Value $env:computername -PassThru
$a = $a | Add-Member -MemberType ScriptProperty -Name Time -Value { Get-Date } -PassThru

$a
$a.Origin
$a.Time

Twitter This Tip! ReTweet this Tip!