Table vs List
When you are using the Select-Object cmdlet, you might have noticed that the results are sometimes displayed as a table and sometimes as a list. It is handy to understand what the logic behind this is.
The basic rule is quite simple, if you display less than 5 properties, the PowerShell output engine will display the results as a table.
1 |
Get-VM -Name TS? | Select Name,PowerState,NumCpu,MemoryGB |
Results in
When there are 5 or more properties, the output will be displayed as a list.
1 |
Get-VM -Name TS? | Select Name,PowerState,NumCpu,MemoryGB,UsedSpaceGB |
Another rule worth knowing, is that the first Select-Object output determines how the other Select-Object outputs in your script will be displayed.
The 1st one shows 5 properties, so it will be displayed as a list. But also the 2nd one, which only displays 3 properties, will also be displayed as a list.
1 2 |
Get-VM -Name TS? | Select Name,PowerState,NumCpu,MemoryGB,UsedSpaceGB Get-VM -Name TS? | Select Name,PowerState,NumCpu,MemoryGB |
When you do the reverse, you will notice that the output is in table format, but the 5th property will not be shown.
1 2 |
Get-VM -Name TS? | Select Name,PowerState,NumCpu,MemoryGB Get-VM -Name TS? | Select Name,PowerState,NumCpu,MemoryGB,UsedSpaceGB |
One way to avoid that the 1st Select-Object determines how the others in your script behave, is to use the Out-Default cmdlet.
This cmdlet kind of “resets” the output, and so the 5-properties rules will be applied as if it was the first Select-Object.
1 2 |
Get-VM -Name TS? | Select Name,PowerState,NumCpu,MemoryGB | Out-Default Get-VM -Name TS? | Select Name,PowerState,NumCpu,MemoryGB,UsedSpaceGB |