liveBook · Manning

Custom type name

As explained in Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

A custom type name is an application-defined identifier assigned to an object type and structured to avoid collisions with existing framework types.

A custom type name is an application-defined identifier placed at the front of an object's type-name list. PowerShell can use it to select a tailored display view instead of a generic one. section 24.2.2

Choose and apply the name carefully:

  • Create a namespace-like hierarchy that your organization controls, such as MyCompany.ITOps.Storage.DiskInfo; avoid names beginning with System. section 24.2.2
  • Insert the identifier at position zero in the object's type-name collection before writing the object to the pipeline. section 24.2.2
  • Match the identifier in a format XML file's TypeName element so the intended table view is selected. section 24.2.3

Disk information output

A Get-DiskInfo function creates each result as a PSObject, inserts Toolmaking.DiskInfo into its type names, and emits it. A format view then selects Toolmaking.DiskInfo and displays Host, DC, Model, and RAM columns. section 24.2.2 section 24.2.3

Listing 24.2 Adding a custom type name to an object
function Get-DiskInfo {
 foreach ($domain in (Get-ADForest).domains) { 
   $hosts = Get-ADDomainController -filter * -server $domain | 
   Sort-Object -Prop hostname
   ForEach ($h in $hosts) { 
    $cs = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $h
    $props = @{'ComputerName' = $h
               'DomainController' = $h
               'Manufacturer' = $cs.manufacturer
               'Model' = $cs.model
               'TotalPhysicalMemory(GB)'=$cs.totalphysicalmemory / 1GB
               }
     $obj = New-Object -Type PSObject -Prop $props              #1
     $obj.psobject.typenames.insert(0,'Toolmaking.DiskInfo')    #2
     Write-Output $obj
   } #foreach $h
  } #foreach $domain
} #function
Export-ModuleMember -function Get-DiskInfo

Inspect the complete PowerShell listing that assigns Toolmaking.DiskInfo to each output object. section 24.2.2

How Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools covers it

How deep it goes
Shows assignment and format-view matching, while stopping short of broader type-system internals.
Examples
Uses Get-DiskInfo, Toolmaking.DiskInfo, and MachineInfo format configurations.
What it assumes
Assumes familiarity with PowerShell objects, pipelines, and basic XML view files.
What you can do after
Apply it via PowerShell object construction and XML formatting configuration.
How it connects
Connects PowerShell objects, namespaces, format files, and modules.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools. The section links point to the parts of the book it draws on.