Faking Object Type

by Oct 22, 2014

PowerShell 3 and later

The internal PowerShell ETS is responsible for converting objects to text. To do this, it looks for a property called “PSTypeName”. You can add this property to your own objects to mimic another object type and make the ETS display your object in the same way:

$object = [PSCustomObject]@{
  ProcessName = 'notepad'
  ID = -1
  PSTypeName = 'System.Diagnostics.Process'

The object pretends to be a process object, and ETS will format it accordingly:

PS> $object

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName          
-------  ------    -----      ----- -----   ------     -- -----------          
              0        0          0     0              -1 notepad              


Twitter This Tip! ReTweet this Tip!