PowerShell has a so-called PSObject, which is a wrapper around any .NET object (or it can be a fully customizable object), and when you call Add-Member, PowerShell implicitly wraps the real .NET object with PSObject.
The way Add-Member works depends on whether or not it started with PSObject. If you have not started with PSObject, Add-Member will wrap the input in PSObject, and you will need to re-assign the variable to see the adapted object.
So for example:
$x = [Environment]::OSVersion $x | Add-Member NoteProperty IsVista $true $x | Format-List
This is because OSVersion is not wrapped in PSObject. The Add-Member element wraps it, but this wrapper is lost because you are not reinstalling $ x on the wrapped object. Contrast with this behavior:
$x = New-Object OperatingSystem ('Win32NT', '6.0') $x | Add-Member NoteProperty IsVista $true $x | Format-List
This is because New-Object implicitly wraps a new instance in PSObject. Thus, your Add-Member call adds members to an existing shell.
Returning to the first example, you can make it work properly by changing it to:
$x = [Environment]::OSVersion $x = $x | Add-Member NoteProperty IsVista $true -PassThru $x | Format-List
Now, after all this, the reason the Hashtable doesnβt work the way you expect is because the Hashtables are handled by special PowerShells, and basically the adapter for the Hashtables uses keys, because the (kinda) and Add-Member properties will not work the way expected with this kind of facility.
Josh
source share