last modified February 15, 2025
In this article, we will cover the Format-Custom cmdlet in
PowerShell. This cmdlet formats output as a custom view defined by the user.
PowerShell provides several formatting cmdlets to control output display.
Format-Custom creates custom views of objects. It uses predefined
views or user-defined property sets. This allows flexible output formatting.
The simplest way to use Format-Custom is with a single object.
It displays the object's properties in a structured view. The output shows
property names and values in a hierarchical format.
Get-Process -Name "notepad" | Format-Custom
This command retrieves Notepad processes and formats them customly. The output shows process details in a structured, indented format.
You can select specific properties to display with Format-Custom.
Use the -Property parameter followed by property names. This creates a focused
view showing only selected properties.
Get-Process -Name "chrome" | Format-Custom -Property Name, Id, CPU
This command shows only Name, ID, and CPU properties for Chrome processes. The output is structured but limited to specified properties.
The -Depth parameter controls how many levels of nested objects are displayed. By default, PowerShell shows up to five levels. This prevents overly deep output. You can adjust this as needed.
Get-Service | Format-Custom -Depth 2
This command displays service information with a maximum depth of 2. Complex objects beyond this depth will be truncated in the output.
Format-Custom can be combined with Select-Object for
more control. First select properties, then format them customly. This provides
flexibility in property selection before formatting.
Get-Process | Select-Object -First 3 -Property Name, Id, WS | Format-Custom
This command selects three processes with specific properties, then formats them. The output shows only selected properties in custom format.
You can create calculated properties for Format-Custom. Use a
hash table with Name and Expression keys. This allows displaying derived
values in the custom view.
Get-Process | Select-Object -First 2 -Property Name, @{
Name="MBMemory"; Expression={$_.WS/1MB}
} | Format-Custom
This command shows process names with working set memory in megabytes. The calculated property converts bytes to MB for readability.
Format-Custom is particularly useful for complex objects. It can
display nested properties clearly. This helps understand object structure.
The output shows relationships between properties.
Get-ChildItem | Select-Object -First 1 | Format-Custom
This command displays detailed information about a file system item. The output shows all properties in a hierarchical, readable format.
You can combine Group-Object with Format-Custom.
First group objects by a property, then format the groups. This creates
structured views of categorized data.
Get-Process | Group-Object -Property Company | Select-Object -First 3 | Format-Custom
This command groups processes by company name and formats the first three groups. The output shows the grouping structure with process details.
In this article, we have covered the Format-Custom cmdlet in PowerShell.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all PowerShell tutorials.