Coding Ref

How to use Format-Table in PowerShell

How to use Format-Table in PowerShell

In PowerShell, you can use the Format-Table cmdlet to format the output of a command as a table.

This cmdlet allows you to specify the columns and their order, and it formats the output as a table with the specified columns and values.

Examples

Example 1

Here's an example of using the Format-Table cmdlet to format the output of a command as a table:

Get-Process | Format-Table ProcessName, CPU

In this example, the Get-Process cmdlet is used to get a list of running processes on the system. The Format-Table cmdlet is used to format the output of this command as a table, with the columns ProcessName and CPU. The code will output a table with the process name and CPU usage for each running process.

Example 2: Automatically adjust the column widths

You can also use the -AutoSize parameter with the Format-Table cmdlet to automatically adjust the column widths to fit the output.

Here's an example:

Get-Process | Format-Table ProcessName, CPU -AutoSize

In this example, the Format-Table cmdlet is used with the -AutoSize parameter. This parameter tells the cmdlet to automatically adjust the column widths to fit the output. The code will output a table with the process name and CPU usage for each running process, and the columns will be automatically sized to fit the output.

Conclusion

The Format-Table cmdlet is a useful tool for formatting the output of a command as a table in PowerShell. It allows you to specify the columns and their order, and it formats the output as a table with the specified columns and values.

You'll also like

Related tutorials curated for you

    How to get the host or computer name in PowerShell

    How to ping in PowerShell

    What are the different PowerShell file types?

    How to use -filter in Get-ChildItem in PowerShell

    Using -Or in PowerShell

    How to get the current directory in PowerShell

    Substrings in PowerShell

    How to create new items in PowerShell

    How to add a user to a group in PowerShell

    How to check for equality in PowerShell

    How to restart a service in PowerShell

    How to Sign a PowerShell Script