A formatting system turns structured PowerShell objects into display instructions. It chooses tables or lists, supplies headers, and controls presentation without changing the underlying objects. section 3.4
Use formatting as a presentation layer rather than embedding display work in commands:
- Return useful objects from tools and let the formatting system decide how they appear. section 3.4
- Create a default view when output should consistently appear as a table or expose selected properties. section 24.1 section 24.2
- Define advanced views with XML-based format files and connect them to a module manifest. section 24.4
Default view for disk information
A Get-DiskInfo function returns computer, domain controller, manufacturer, model, and memory properties. A formatting view can render these objects as a table automatically, without piping to Format-Table or altering the returned data. section 24.1 section 24.2
Listing 24.1 Starting point for this chapter
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
}
New-Object -Type PSObject -Prop $props
} #foreach $h
} #foreach $domain
} #function
Export-ModuleMember -function Get-DiskInfo
Inspect the Get-DiskInfo implementation and its object-producing output. section 24.1






