Format Custom - PowerShell


Overview

Format-Custom is a PowerShell command that provides advanced formatting options for tabular data. It allows you to create customized, visually appealing tables with rich formatting and dynamic content. This command is particularly useful for presenting complex or hierarchical data in a structured and readable manner.

Syntax

Format-Custom -InputObject <object> -Property <string[]> -Value <object[]>

Options/Flags

  • -InputObject: Specifies the object or collection of objects to be formatted.
  • -Property: An array of property names to include in the table columns.
  • -Value: An array of values corresponding to the specified properties.

Examples

Simple Example:

$obj = Get-Process
Format-Custom -InputObject $obj -Property 'Name', 'Id', 'Threads'

This command retrieves a list of running processes and creates a table with columns for the process name, ID, and number of threads.

Dynamic Content Example:

$obj = Get-WmiObject Win32_LogicalDisk
Format-Custom -InputObject $obj -Property 'SystemName', {Label "DiskLetter"}, 'Capacity'

This command retrieves information about logical disks on the system and creates a table with a customized label for the “DiskLetter” column.

Hierarchical Example:

$obj = Get-ChildItem
Format-Custom -InputObject $obj -Property 'Name', 'DirectoryName' -Value @(
  @('ChildItem 1', 'Subdir A'),
  @('ChildItem 2', 'Subdir B')
)

This command creates a hierarchical table that lists child items and their corresponding directory names. The values for the second column are specified using script blocks to create nested arrays.

Common Issues

  • Missing Property: If the specified property does not exist in the input object, the value column will be empty for that property.
  • Incorrect Value Count: The number of values must match the number of properties specified. If there are more values than properties, the extra values will be ignored. If there are fewer values, the remaining columns will be empty.

Integration

Pipeline Integration: Format-Custom can be used as part of a PowerShell pipeline to further process or display the formatted data.

Exporting to File: The formatted table can be exported to files such as CSV, HTML, or XML using the Export-Csv, Export-Html, or Export-Xml commands, respectively.

  • Format-Table: Basic table formatting.
  • ConvertTo-Html: Converts objects to HTML tables.
  • ConvertTo-Csv: Converts objects to CSV files.
  • ConvertTo-Xml: Converts objects to XML files.