Updated March 24, 2023
Introduction to PowerShell Format Table
We have many commands to see different types of outputs, but what if we need the output in a certain format that is more readable and understandable? So for that, we can use Powershell Format-Table, This commands display output in the format that we need. Let’s take one example, suppose we are going to display specific columns names like name, date, and CPU, etc, then we will use the command Format-Table. A normal command like Get-process will display all the columns which you may not require, but Get-process along with command Format-Table command will give only specific columns that we are needed. So with the help of Format-Table command, we can arrange and show only those columns which are necessary to see. This command is very useful where we wanted to show only limited details for better clarity of outputs. In Examples, we will see various types of uses and situations where we can use Format-Tablecommands to customize output format.
How to Format the Table in PowerShell?
See the below syntax we use for formatting our outputs.
Syntax:
Format-Table
[-AutoSize(auto adjust output)]
[-RepeatHeader(After each new screen show header for clarity)]
[-HideTableHeaders(Delete the column overlapping from output structure)]
[-Wrap(Show the details in the next line if it exceed column width)]
[[-Property] <Show the object properties>]
[-GroupBy <here we can group output in another table based on the properties .>]
[-View <allow us for custom table>]
[-ShowError(just to send error in Pipeline)]
[-DisplayError(show error on command line)]
[<CommonParameters>]
When we will be writing very big scripts at that time we would like to see minimum columns for better understanding, rather than displaying everything. In the below example we are first executing a command Get-Process, to see the whole process and it’s all other properties and output we got PM(K), PM(M), WS(M), CPU(s), ID, SI, Processname as the columns. Here we can see Get-Process is displaying every attribute of process, you may not be able to understand every property.
Get-Process
Output:
But this output is a little confusing as it showing all columns. Let’s assume we are only interested in the ProcessName field than what? Here we will use Format-Table command to get specific columns that we are looking for. Let’s write an example for it. see the below example.
Get-Process | Format-Table ProcessName, Handles -auto
Output:
In the above example, we are only displaying ProcessName.You can write your own command to get only process Id or CPU details.
Examples of PowerShell Format Table
Below are the examples of PowerShell Format Table:
Example #1
We have discussed various ways to use Format-Table command, now let’s explore little more utility about it. Suppose we want to get a formatted table with sorted by their start date. You can see screen output for reference. Here we are getting all the processes available inside our system along with sorted by their start time.
Get-Process | Sort-Object StartTime | Format-Table -View StartTime
Output:
Example #2
If We want to see today’s date along with the display error. Many times to get a better understanding of errors we can use this command.
Get-Date | Format-Table DayOfWeek,{ $_ / $null } -DisplayError
Output:
The output is showing Thursday along with column #ERR. You can also try it. You will get different output depending on the date on which you are running this command. These practices are good to use if it is expected to give some wrong outputs.
Example #3
Many times we want to see our output in the group by, for example, suppose you want to see the all group by an inactive process to kill them Or all the process started on the same group by date, Or you may need to see all the process on the basis of the group by basepriority. Let’s see the below example. In this example, we are formatting our output group by basepriority.
get-process | format-table -groupby basepriority
Output:
Let’s take another example of the group by, where we are displaying all the output groups by their status. Please see the below example along with the screen. You can try more group by according to the properties of services. Always raise a question that why you need this command. Assume your application is running very slow because of many inactive processes. So you can write a command group buy status and kill all the inactive processes to improve your system performance.
get-process | format-table -groupby status
Output:
Group by can also be used for file and folder system, for example, suppose you want to see all the file in formatted way of it’s last used, group by last modified. Here we are passing LastAccessTime as an argument to Group by. Let me tell you one example suppose you have a folder where 1000 files are there and these files are created on a daily basis, now if you want to see newly created or modified files group by particular dates, then it will be very easy to do with Groupby command. Please refer to the below example with the screen.
Get-ChildItem ./ranjan | Format-List -GroupBy LastAccessTime
Output:
Example #4
Let’s write a command which will show all the properties of a folder in the new line. See the below example, here we are formatting output for ranjan folder and all the output we are getting in the newline. Here we can also define or restrict the number of columns to be displayed in the newline.
Get-Item -Path ./ranjan | Format-List
Output:
Example #5
You can use alias also in PowerShell, For example in PowerShell to write format-table in short form, we can write FT. Let me tell you one example where we are going to use alias FT. In the below example we are using FT to get all processes with only three columns, processName, Handle and PagedMemorySize. It will be useful if you are writing a big script, where you can use a smaller name to make your script smaller.
Get-Process | FT ProcessName, Handles, PagedMemorySize -auto
Output:
Example #6
We can also show the output in more than one column. Here we have a command called format-wide (fw). This command will be useful if you are going to display output with a very long list but this long list contains only one field.
Get-Process | Get-Member -Membertype method | Format-Wide name -column 3
Output:
Conclusion
Format-Table is very useful where we have a very large and complex output format, in such type of situation we can define our own format to minified or modified format of output for better understanding.
Recommended Articles
This is a guide to PowerShell Format Table. Here we discuss the introduction and how to format the table in PowerShell along with the examples. You may also look at the following articles to learn more –