How PowerShell Formatting and Outputting REALLY works

Dreeschkind posted a question in the Microsoft.Public.Windows.Server.Scripting newsgroup about how PowerShell formatting worked with Select. He saw some behavior that he thought was a bug. Here is what he saw:

Notice the lack of a column for StartTime. It appears that we’ve lost information. We haven’t and as Wei Wu’s response shows, you can easily get what you want by asking for it explicitly (by piping it to FT and specifying the properties you want):

This is a good setup to drill into how formatting works. This is a black belt dive into formatting and outputting and not for everyone so don’t continue unless you want to know how things REALLY work.

The vast majority of PowerShell is implemented by System.Management.Automation.dll. PowerShell.Exe is an unmanaged executable which determines the environment that is required and dynamically loads the correct CLR and managed code PowerShell host (Microsoft.PowerShell.ConsoleHost.dll). Control is passed to this DLL which provides the user experience. This is a relatively small DLL providing a console experience of the core engine. It calls the engine to get the prompt but then it displays it and collects user input. It then takes that input, concatenates ” | Out-Default” and submits it to the engine for execution. … YES, under the covers every command entered in through the console host is piped to Out-Default.

Out-Default is responsible for figuring out how to format and output the object stream. If the object stream is a stream of strings, Out-Default pipes these directly to Out-Host which calls the appropriate APIs provided by the host (hosts have to provide an interface to the engine, in this case Microsoft.PowerShell.ConsoleHost.dll provides implementations which send the output to the Console). If the object stream does not contain strings, Out-Default inspects the object to determine what to do. First it looks at the object type and determines whether there is a registered VIEW for this object type.

PowerShell defines XML schema and a mechanism (the Update-FormatData cmdlet) where anyone can register views for an object type. You can specify WIDE, LIST, TABLE, or CUSTOM views for any object type. The views specify which properties to display and how they should be displayed. If a view is registered, it defines which FORMATTER to use. So if the registered view is a TABLE view, OUT-DEFAULT streams the objects to “Format-Table | Out-Host”. Format-Table transforms the objects into a stream of Formatting records (driven by the data in the View definition) and Out-Host transforms the formatting records into calls on the Host interface. You can see this for yourself by observing that you’ll get the same display by typing any of the following:

NOTE: Out-String transforms formatting records to a string and then Out-Default passes them unmolested to Out-Host.

You can also see the formatting records by doing the following:

Get-Process |Format-Table |Get-Member

Now, if there is not a registered view for a datatype, then Out-Default looks at the FIRST OBJECT IN THE STREAM to determine how many properties the object has 5 or more properties, it send the ENTIRE STREAM to Format-List, otherwise it sends the ENTIRE STREAM to Format-Table. When it sends the stream to Format-Table, that command needs to generate columns. It does this by looking at the properties of the FIRST OBJECT – those become the columns. If the first Object has 2 properties, you’ll get a 2 column table even if all the other objects in the stream have 10 properties.

This is what is happening to Dreeschkind. Go back and look at the original example and you’ll notice that there was an error accessing StartTime during Sort and Select-Object. What that is there was an ACCESS WAS DENIED error while accessing StartTime for the Idle process. When this occurred, sort put this object at the front of the stream and Select did not create a property for StartTime for this result object. Thus an object that did not have a StartTime property was the first object in the stream. Since there were fewer than 5 properties, the objects where sent to Format-Table which created columns based upon the first object and since it did not have StartTime – there was no column created for it. Had the objects not been sorted by StartTime or if it had been a DESCENDING sort, the first object would have had a StartTime property and everything would have been fine. Alternatively, if you pipe the objects to Format-Table and specify the fields you want, it would have done what you wanted. This creates columns and if the object doesn’t have that field – the row has a blank in that column.

It is interesting to note that if you and selected enough properties, Out-Default would have sent the object stream to Format-list and then each object would have all of its properties displayed.

In the end, the fact that select did not create a property when it encountered a problem is probably a bug and should be fixed. That said, this allowed us to explore the fascinating inner workings of formatting and outputting so something good came of it.

EnjoyJeffrey SnoverPowerShell Architect

PSMDTAG:INTERNAL: How formating decides which metadata to use, default formatters and outputtersPSMDTAG:CMDLET:FORMAT: How formating decides which metadata to use, default formatters and outputters

Reading this article, I thought wow what a terrific insight into how PS works, and so well articulated! Then when I reached the bottom and saw Jeffrey Snover's name it all made sense. Thank you for sharing sir.

6 years ago

Don Lassini

This is simply a marvelous and much needed description. Thank you!

9 years ago

Tibor Soos

This post is quite old, but it is useful. It’s now PS 2.0, and I do not understand the following behaviour:

$p = @{e=1;k=2}

$x = New-Object -Property $p -TypeName psobject

$x, (Get-Process powershell) | ft

k e

– –

2 1

(There is no process object displayed.)

[21] PS C:> (Get-Process powershell),$x | Format-Table

Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName

——- —— —– —– —– —— — ———–

807 23 49452 50408 570 4,53 3412 powershell

k : 2

e : 1

(There are both the process object and the custom object displayed, process is in table format, custom object is in list format.)

Why?

11 years ago

marco_shaw@hotmail.com

Either you modify the default .ps1xml files (**not recommended**) or you simply drop your update-formatdata into your profile to run it everytime (notepad $profile).