

PowerShell Rules For Format-Table And Format-List
source link: https://thomasrayner.ca/powershell-rules-for-format-table-and-format-list/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

PowerShell Rules For Format-Table And Format-List
In PowerShell, when outputting data to the console, it’s typically either organized into a table or a list. You can force output to take either of these forms using the Format-Table and the Format-List cmdlets, and people who write PowerShell cmdlets and modules can take special steps to make sure their output is formatted as they desire. But, when no developer has specifically asked for a formatted output (for example, by using a .format.ps1xml file to define how an object is formatted), how does PowerShell choose to display a table or a list?
The answer is actually pretty simple and I’m going to highlight it with an example. Take a look at the following piece of code.
PS> get-wmiobject -class win32_operatingsystem | select pscomputername,caption,osarch*,registereduser
PSComputerName caption OSArchitecture registereduser
-------------- ------- -------------- --------------
workingsysadmin Microsoft Windows 10 Enterprise 64-bit [email protected]
I used Get-WmiObject to get some information about my operating system. I selected four properties and PowerShell decided to display a table. Now, let’s add another property to return.
PS> get-wmiobject -class win32_operatingsystem | select pscomputername,caption,osarch*,registereduser,version
PSComputerName : workingsysadmin
caption : Microsoft Windows 10 Enterprise
OSArchitecture : 64-bit
registereduser : [email protected]
version : 10.0.14393
Whoa, now we get a list. What gives?
Well here’s how PowerShell decides, by default, whether to display a list or table:
- If showing four or fewer properties, show a table
- If showing five or more properties, show a list
That’s it, that’s how PowerShell decides by default whether to show you a list or table.
Recommend
-
13
Format-Graph CmdLet; Drawing graphics with PowerShell Mar 18, 2007 sy...
-
15
PowerShell script to convert CSV file format to Complex Nested JSON by Kapil Khandelwal · August 11, 2021 · 10 Views Frequently, we get raw d...
-
7
Export/Convert Table or SQL Query data to JSON string format | SQL Server 2016 – Part 1 In my [previous post]
-
9
Tutorial How To List and Delete Iptables Firewall Rules Security
-
9
How to format a list dict as a table advertisements I have a dict containing lists of strings and was wanting...
-
3
Niclas Moldenhauer July 31, 2018 4 minute read
-
14
Michael Redmann May 9, 2023 3 minute read...
-
7
UK Court of Appeal rules Craig Wright’s copyright claim over Bitcoin file format can proceed Business 20 July 2023 Dr. Craig Wright...
-
9
Errors · Excel · ...
-
4
How to Specify a Date Format on Creating a Table and Fill it in SQL? ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK