Adding properties (named values) to a PSCustomObject object
As all
PowerShell objects, a PSCustomObject can be added properties, which are basically named values. This functionality is probably the main use for PSCustomObjects.
$psCustObj = new-object psObject
$psCustObj | add-member num 42
$psCustObj | add-member txt 'hello world'
write-host "num = $($psCustObj.num), txt = $($psCustObj.txt)"
'num', 'txt' | foreach-object { write-host "$_ : $($psCustObj.$_ )" }
Definition and significance of PSCustomObject
In the
source code,
PSCustomObject
is defined like so (redacted by me for brevity):
public class PSCustomObject
{
/// To prevent other instances than SelfInstance.
private PSCustomObject() { }
internal static PSCustomObject SelfInstance = new PSCustomObject();
public override string ToString()
{
return string.Empty;
}
}
So,
PSCustomObject
directly derives from
System.Object
, does not implement any interface and does not have any fields or properties except the
internal
field
SelfInstance
.