You may have heard that PowerShell can add custom properties to objects. While we are not going into much detail about this here, we'd like to explain why this works for some objects and not for others.
Let's assume you pick a process object, in this case any running PowerShell process:
Now, to add a new property, all you would need to do is use Add-Member:
Now, $process has a new property called SayHello and returns "Hello":
When you try the same with a different object such as a string, it fails:
$object | Add-Member NoteProperty SayHello Hello
The truth is PowerShell can add new properties only to its own object types (PSObject). So if the object you want to extend is not of the correct type, Add-Member cannot manipulate it. To solve this issue, use the -passThru parameter and update the variable. This way, Add-Member converts your object into the appropriate type, adds the property and updates your variable:
$object = $object | Add-Member NoteProperty SayHello Hello -passThru