Formatting output in the PowerShell console
Table of Contents
I. Introduction
In this chapter, we'll learn how to format output in the PowerShell console through the use of several cmdlets: Format-Table, Format-List, Select-Object and Out-GridView. These are particularly useful when we're working in the PowerShell console and need to format the output in a certain way. The Select-Object cmdlet also plays an interesting role in scripting.
By default, each PowerShell cmdlet returns objects according to its default display, usually with several properties. As we've seen, each object contains a set of properties, and sometimes we need to select and/or display certain properties rather than others. These various cmdlets will help us achieve our goal!
II. Table display with Format-Table
The Format-Table cmdlet lets you present data in the console in tabular form. This is particularly useful when working with structured data. It is always used after a pipeline to display in the console the objects returned by the first command.
Here's an example of how to display an array with the "Name", "LastLogon" and "SID" properties only. By default, the "Get-LocalUser" cmdlet returns the "Name", "Enabled" and "Description" properties.
Get-LocalUser | Format-Table Name, LastLogon, SID
Which gives:

There is also an alias for the "Format-Table" cmdlet:
Get-LocalUser | ft Name, LastLogon, SID
III. List display with Format-List
The"Format-List" cmdlet works on the same principle as "Format-Table", except that it will display data in list rather than table format.
Using the previous example, we obtain :
Get-LocalUser | Format-List Name, LastLogon, SID
The information is the same, but the presentation is different. Sometimes this is more practical than table mode, especially when we need to display a large number of properties.

There is also an alias for the "Format-List" cmdlet:
Get-LocalUser | fl Name, LastLogon, SID
IV. Selecting properties with Select-Object
The"Select-Object" cmdlet is used to select specific object properties. It is useful in a variety of situations and can be coupled with "Format-Table", "Format-List" and "Out-GridView".
When we execute this command :
$ListeUtilisateurs = Get-LocalUser
PowerShell stores the result returned by "Get-LocalUser" in the"$UserList" variable, along with all its properties. If we need to keep only certain properties, we can do the following:
$ListeUtilisateurs = Get-LocalUser | Select-Object Name, LastLogon, SID
We can see the impact on the contents of the "$UserList" variable:

The"Select-Object" cmdletacts as a filter, allowing you to select one or more properties.
If we use "Select-Object" with the other cmdlets for display formatting, we get :
Get-LocalUser | Select-Object Name, LastLogon, SID | Format-Table
Get-LocalUser | Select-Object Name, LastLogon, SID | Format-List
Get-LocalUser | Select-Object Name, LastLogon, SID | Out-GridView
There is also an alias for the "Select-Object" cmdlet:
Get-LocalUser | select Name, LastLogon, SID
Finally, "Select-Object" can be used to create what are known as calculated properties, i.e. customized properties. To find out more, read this article:
V. A semblance of a graphical interface with Out-GridView
"Out-GridView" is a cmdlet that lets you present data in grid form, in a new interactive window. In fact,"Out-GridView" is very useful for visualizing information graphically, outside the console.
Unlike "Format-Table" and "Format-List", it does not allow you to specify the properties to be displayed. This means we need to use "Select-Object" to select the properties we wish to display.
Get-LocalUser | Out-GridView
Get-LocalUser | Select-Object Name, LastLogon, SID | Out-GridView
This opens a new window with the information corresponding to the selected properties:

We can click on column names to sort according to a specific property, either in ascending or descending order! What's more, we can enter a keyword in the search box to filter data on the fly. The"Add criteria" button lets you add one or more conditions based on the properties visible on the screen. It's so handy!

The "Out-GridView" cmdlet will prove invaluable and save you a lot of time if you have an output with a lot of information.
Note : to sort without using "Out-GridView", we can use the"Sort-Object " cmdlet.
Finally, there's also an alias for the "Out-GridView" cmdlet:
Get-LocalUser | ogv
VI. Conclusion
In conclusion, we can say that the "Format-Table", "Format-List","Select-Object" and "Out-GridView" cmdlets offer great flexibility for the presentation and manipulation of objects in PowerShell, in order to adjust the output in the console. We've also seen that there's a real benefit in combining them in certain cases, notably when using "Select-Object" and "Out-GridView" together.