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
