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
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.
First the representation:
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].
-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
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.
you need to add
-Widthto it. Something like this:
.\List-Delphi-Installed-Packages.ps1 ProductSummaries | Out-File -FilePath c:\temp\Delphi.txt -Width 500
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
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]
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
But first an observation of how the
-Width parameter of
Out-File interacts with
The pipeline works both ways
Out-File solution shows that:
the pipeline works both ways: objects stream down the pipeline, but a
-Widthsetting 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].
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
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).
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
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.
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.