Dynamic Type Formatting in PowerShell
In this issue:
Today I want to show you a PowerShell tool that you might find very useful. Not only in your daily PowerShell work, but also in your module development. Especially for functions that write custom objects to the pipeline and you want to control how these objects are displayed. I write a lot about objects and ways to present them to the user.
It is very important that you separate the data in the object to how it is formatted and presented. You want to write the richest possible object to the pipeline. Separately, you can decide how to format not only a default display, but also alternative views. PowerShell provides a way to do this using formatting files (.ps1xml). In the past you've seen me use New-PSFormatXML from the PSScriptTools module to create formatting files. But, that's not the only tool available to you.
TypeFormat
A few years ago PowerShell MVP Justin Grote published a GitHub gist with code that you could run to dynamically create custom formatting and automatically apply it to your session. Eventually, he turned this into a module called TypeFormat and published it to the PowerShell Gallery.
PS C:\> Find-PSResource TypeFormat
Name Version Prerelease Repository Description
---- ------- ---------- ---------- -----------
TypeFormat 0.0.1 PSGallery Create and Persist PowerShell formatting files using For...
Unfortunately, Justin started to setup a GitHub repository for the module, but never finished so there's no place to file issues or learn more. Still, the core functionality works and I'll demonstrate a way around one of the major limitations of the module.
Even though it isn't documented, as written, the module requires PowerShell 7. Install the module from the PowerShell Gallery.
Install-PSResource TypeFormat
For testing purposes, I recommend you use a separate PowerShell 7 session with no profile.
pwsh -NoProfile -NoLogo
The module has a single public function.
PS C:\> Import-Module TypeFormat
PS C:\> Get-Command -Module TypeFormat
CommandType Name Version Source
----------- ---- ------- ------
Function Format-TypeTable 0.0.1 TypeFormat
Dynamic Formatting
The concept is straightforward and easy to understand. You are probably familiar with using Format-Table like this to create a custom formatted view.
PS C:\> dir c:\temp\*.zip |
>> Format-Table -GroupBy Directory -Property LastWriteTime,CreationTime,
>> @{Name = 'SizeKB'; Expression = { $_.length/1KB } },Name
Directory: C:\temp
LastWriteTime CreationTime SizeKB Name
------------- ------------ ------ ----
5/28/2025 1:26:34 PM 5/28/2025 1:26:34 PM 570.70 ADSync.zip
7/13/2022 3:32:38 PM 6/14/2025 3:43:49 PM 370.88 DeDRM_plugin.zip
5/20/2025 10:15:02 AM 5/20/2025 10:10:12 AM 96.74 GraphicsFiles.zip
11/16/2025 7:08:12 PM 11/16/2025 7:08:12 PM 69.34 jh-musicxml.zip
7/13/2022 3:32:38 PM 6/14/2025 3:43:49 PM 70.36 Obok_plugin.zip
6/4/2025 1:09:13 PM 5/20/2025 10:21:05 AM 97.29 png.zip
However, if you always want to see this output, you have to re-type the command. Or, you can use the same syntax with Format-TypeTable.
dir c:\temp\*.zip |
Format-TypeTable -Property LastWriteTime,CreationTime ,
@{Name = 'SizeKB'; Expression = { $_.length/1KB } }, Name -GroupBy Directory
The output will be the same. Now for the good news/bad news. You don't have to re-type the command to get the formatting.
PS C:\> dir c:\scripts\my*.zip
Directory: C:\scripts
LastWriteTime CreationTime SizeKB Name
------------- ------------ ------ ----
2/10/2016 1:09:55 PM 2/10/2016 1:09:55 PM 8.705078125 MyCompositeResource.zip
2/10/2016 7:19:29 PM 2/10/2016 7:19:29 PM 6.4296875 MyJumpResource.zip
2/9/2016 5:10:51 PM 2/9/2016 5:10:51 PM 6.044921875 MyLOBResource.zip
1/8/2015 12:26:10 PM 1/8/2015 12:26:10 PM 11.3896484375 MyMonitor.zip
2/3/2016 6:52:53 PM 2/3/2016 6:52:53 PM 10.5517578125 MyPageFileSetting.zip
2/2/2016 7:03:03 PM 2/2/2016 7:03:03 PM 4.1708984375 MySimpleResource.zip
2/3/2016 8:19:12 AM 2/3/2016 8:19:12 AM 5.595703125 MyWindowsFeature.zip
2/1/2016 2:11:01 PM 2/1/2016 2:11:01 PM 2.9296875 MyWindowsFeatureResource.zip
I have changed the default formatting for all FileInfo objects in my session. Every time I run a command that outputs FileInfo objects, they will be formatted using my custom view. This may or may not be what you want.
You can use the default view by specifying the view name.
PS C:\> dir c:\scripts\my*.zip | Format-Table -View children
Directory: C:\scripts
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2/10/2016 1:09 PM 8914 MyCompositeResource.zip
-a--- 2/10/2016 7:19 PM 6584 MyJumpResource.zip
-a--- 2/9/2016 5:10 PM 6190 MyLOBResource.zip
-a--- 1/8/2015 12:26 PM 11663 MyMonitor.zip
-a--- 2/3/2016 6:52 PM 10805 MyPageFileSetting.zip
-a--- 2/2/2016 7:03 PM 4271 MySimpleResource.zip
-a--- 2/3/2016 8:19 AM 5730 MyWindowsFeature.zip
-a--- 2/1/2016 2:11 PM 3000 MyWindowsFeatureResource.zip
Once you close the session, the formatting is lost.
There is no way to update an existing formatting definition. You will have to restart your PowerShell session.