Every PowerShell object is of some data type. For example, the process object is an instance of System.Diagnostics.Process type.
Get-Process | Get-Member
TypeName: System.Diagnostics.Process ...
Objects that result from PowerShell commands execution belong to some data type, but this doesn’t prevent us from adding a custom type.
It becomes even more useful when we work with custom objects – System.Management.Automation.PSCustomObject.
[PSCustomObject]@{ Name = 'Object' Description = 'Object Description' }
Why do we need it?
If we define some objects as ones that belong to some custom type, we then can refer to it in type and format files – types.ps1xml and format.ps1xml, respectively, and define additional properties, methods, and views.
But in this article we will concentrate on techniques of adding custom types to objects and will not describe purpose and structure of types and format files.
PSTypeNames
First, let’s discuss, how we can find out, what type and object belongs to.
As we saw in the first example, we can use the Get-Member cmdlet. But it returns only most specific type and not the whole type hierarchy. To find out, what types an object inherits from, we can use the pstypenames property.
It is needed to say that, because the pstypenames is a hidden property, autocompletion functionality by using Tab kay or Ctrl+Space shortcut will not be available.
$Process = Get-Process -Id $pid $Process.pstypenames
System.Diagnostics.Process System.ComponentModel.Component System.MarshalByRefObject System.Object
The same applies to PSCustomObject.
$PSCustomObject = [PSCustomObject]@{ Name = 'Object' Description = 'Object Description' } $PSCustomObject.pstypenames
System.Management.Automation.PSCustomObject System.Object
Add-Member
One of the approaches to adding custom type to objects is using the Add-Member cmdlet.
$PSCustomObject | Add-Member -TypeName 'CustomType' $PSCustomObject.pstypenames
CustomType System.Management.Automation.PSCustomObject System.Object
Type collection
Another way is to use the value of pstypenames property, which data type inherits from the string collection. So, the following methods are available: Add, Insert, Remove, RemoveAt, and Clear.
Add
$PSCustomObject.pstypenames.Add("AnotherCustomType") $PSCustomObject.pstypenames
CustomType System.Management.Automation.PSCustomObject System.Object AnotherCustomType
Remove
$PSCustomObject.pstypenames.Remove("AnotherCustomType") $PSCustomObject.pstypenames
CustomType System.Management.Automation.PSCustomObject System.Object
RemoveAt
$PSCustomObject.pstypenames.RemoveAt(0) $PSCustomObject.pstypenames
System.Management.Automation.PSCustomObject System.Object
Insert
$PSCustomObject.pstypenames.Insert(0, "AnotherCustomType") $PSCustomObject.pstypenames
AnotherCustomType System.Management.Automation.PSCustomObject System.Object
Clear
$PSCustomObject.pstypenames.Clear() $PSCustomObject.pstypenames
PSTypeName property
Also, in case of a custom object, we can define its type at the creation, by specifying the PSTypeName property.
$PSCustomObject = [PSCustomObject]@{ Name = 'Object' Description = 'Object Description' PSTypeName = 'OneMoreCustomType' } $PSCustomObject
Name Description ---- ----------- Object Object Description
$PSCustomObject.pstypenames
OneMoreCustomType System.Management.Automation.PSCustomObject System.Object
Return
In this article we have discussed several approaches to adding custom data types to objects, which is one of the steps to extend available set of types and formats in PowerShell.
2 thoughts on “Adding custom types to PowerShell objects”