PowerShell: when Format-Table -AutoSize displays only 10 columns and uses the width of the console when redirecting to file
Posted by jpluimers on 2017/03/09
Lets start with the second problem: There are various ways to redirect PowerShell output to a file.
- Shell redirect with a greater than sign (
>
) to create/overwrite output or two greater than signs (>>
) to append output. - Use Out-File [WayBack] with a filename and either
-FilePath
(default, similar to>
) or-Append
(similar>>
).
I write “similar” as they are not fully equivalent. That’s where Format-Table [WayBack] with the -AutoSize
parameter comes in (with or without a -Wrap
parameter).
Apart from Format-Table displaying only 10 columns by default (see below), the -AutoSize
will change columns presentation depending not just on the -Wrap
parameter but also to the total width it thinks it has available.
Useful Format-Table
parameters
First the representation:
-Wrap
Indicates that the cmdlet displays text that exceeds the column width on the next line. By default, text that exceeds the column width is truncated.
The Microsoft documentation does not have screenshots, but basically “truncated” (without the
-Wrap
parameter) means that columns will end in ellipsis (…) when truncated as shown in powershell – Can’t see full key names in registry using Format-Table command – Stack Overflow [WayBack].
With the -Wrap
parameter a “cell” will be wrapped over multiple lines which messes up the output if you have one or few wide columns mixed with many narrow columns.
To circumvent that, you can use the -AutoSize
parameter:
-AutoSize
Indicates that the cmdlet adjusts the column size and number of columns based on the width of the data. By default, the column size and number are determined by the view.
What happens with -AutoSize
is that narrower columns will take up less space and wider columns with one caveat: it will not make the whole table wider than the console width where PowerShell is running on.
Oh and Format-Table shows only 10 columns at maximum, but more on that later. First the width problem,Phry [WayBack] for pointing that out to me:
you need to add
-Width
to it. Something like this:.\List-Delphi-Installed-Packages.ps1 ProductSummaries | Out-File -FilePath c:\temp\Delphi.txt -Width 500
This uses Out-File
with a -Width
parameter and immediately puts the output in a file.
In my case, I now had to change this cmd statement:
powershell -f .\List-Delphi-Installed-Packages.ps1 ProductSummaries > output.txt
into:
powershell -Command ".\List-Delphi-Installed-Packages.ps1 ProductSummaries | Out-File -Width 500 tmp1.txt"
I didn’t like it much, but it works.
It works because of what’s in these two posts:
- For Love of Software: Calling a PowerShell script in a path with a white space from command line [WayBack]
- batch file – Run PowerShell command from command prompt (no ps1 script) – Stack Overflow [WayBack]
Both talk about the -Command
parameter on the PowerShell command-line [WayBack] that allows you to pass a full command – including any piping performed by PowerShell – from the cmd console:
-Command
Executes the specified commands (and any parameters) as though they were typed at the Windows PowerShell command prompt, and then exits, unless the NoExit parameter is specified.
I’m not sure why I didn’t need to use the &
(ampersand) block operator as technically it executes a block statement. Maybe that will be needed when putting spaces in the path. I don’t need to because of an even better solution below: the Out-String
command.
But first an observation of how the -Width
parameter of Out-File
interacts with Format-Table
:
The pipeline works both ways
Basically the Out-File
solution shows that:
the pipeline works both ways: objects stream down the pipeline, but a
-Width
setting trickles up the pipeline.
Out-String
is a better solution
Later, when I was looking for screenshots in this article, I found another article that basically explains a different way to work around the width issue: PowerShell Quick Tip: Creating wide tables with PowerShell | Poshoholic [WayBack].
There, Kirk Munro [WayBack] provides an even better solution. And explains the solution below is about the -Property
parameter (which is the default parameter for Format-Table
) which is cryptic:
-Property<Object[]>
Specifies the object properties that appear in the display and the order in which they appear. Type one or more property names (separated by commas), or use a hash table to display a calculated property. Wildcards are permitted.
If you omit this parameter, the properties that appear in the display depend on the object being displayed. The parameter name (Property) is optional. You cannot use the Property and View parameters in the same command.
The value of the Property parameter can be a new calculated property. To create a calculated property, use a hash table. Valid keys are:
— Expression <string> or <script block>
— Depth <int32>
The clue here is “Wildcards are permitted.” which is not explained any further but means “include all properties”.
Back to Out-String which is a much better solution and is part of the Out- family of functions that sends output into various directions:
- Out-File [WayBack] – Sends output to a file.
- Out-String [WayBack] – Sends objects to the host as a series of strings.
- Out-GridView [WayBack] – Sends output to an interactive table in a separate window.
- Out-Printer [WayBack] – Sends output to a printer.
The cool thing is that like Out-File
supports the -Width
parameter, Out-String
does too *and* outputs to the host, so can continue the pipeline (and therefore be part of any other pipeline processing including shell redirection).
His solution:
Get-Alias -Definition Invoke-* ` | Format-Table -Property * -AutoSize ` | Out-String -Width 4096 ` | Out-File C:\aliases.txt
The cool thing is that this works with and without redirection and is neutral to either PowerShell or regular redirection. Without redirection, it takes whatever Width it needs up to 4096 characters (which can be made larger when needed). With a less wide console, the lines will just wrap which is OK for me.
Showing more than 10 columns with Format-Table
I solved this by asking What’s the maximum number of columns for Format-Table cmdlet in PowerShell – Stack Overflow [WayBack].
Basically it comes down to Format-Table
having an undocumented parameter *
(yes, that parameter is a star or asterisk character, not even documented in common-parameters [WayBack]) that specifies you want an unlimited number of parameters. Thanks again Phry [WayBack] for pointing that out to me and indicating the default number of columns is 10.
–jeroen
PS: the script involved was the continued development of PowerShell script to show the component packages (BPL) files for all installed Delphi (actually: BDS) versions.
alendarflux said
You are a great god of PowerShell. I haven’t found this secret “*” parameter anywhere else.