Write to Table in Powershell

This article uses examples to discuss different methods of writing data into a table in PowerShell. In particular, we will discuss how to write user-defined data to a PowerShell table, read data from a file and write it to a table, format PowerShell output as a table, and write output into a tabular form data file like a CSV.

The code in this article will be saved on a PowerShell script and executed as such. To execute a PowerShell script, you need to save code in a .ps1 file, e.g., file1.ps1, and execute the code from PowerShell with the command

Example 1: Writing data into PowerShell Table

The first way to write data into a table in PowerShell is to use the New-Object cmdlet to create a System.Data.DataTable object and write data into it.

File: write_to_table1.ps1

Example 2: Reading Data from a Text File and Writing them to Table

In this example, we are reading the data from data1.txt with the contents shown below. The first row is the header row, and the data items are separated by a semi-colon.

Walsh;Mumbai, India;CFO
Osteen Thee;Berkley, Carlifonia;Managing Director
Victoria Smith; Texas, USA;CEO

The following PowerShell code reads the contents of the data1.txt file and outputs the data as a table.

File: write_to_table2.ps1

In the code above, we use the Get-Content cmdlet to read the contents of data1.txt. Select-Object -Skip 1 is used to skip the first row of the data to prevent the header row from being written twice.

ForEach-Object loops through each line of the file and splits it using the semi-colon (;) to get the data points on each row.

[PSCustomObject] allows us to create custom objects with three properties Name, Address, and Role, and assign corresponding values of the split to each property.

After creating the custom objects, we pipe them to Format-Table to create a table. The -AutoSize parameter is used to resize the columns to fit the data dynamically.

We could also use System.Data.DataTable object in this example to write the contents of data1.txt into a PowerShell Table.

Since we know the delimiter for our data, we could also use Import-Csv to read data and then format the table using the Format-Table cmdlet.

With Import-Csv, you can define custom headers using the -Header parameter. If this parameter is not passed, the first row is used as the column headers.

Example 3: Sending the Output in Example 2 into CSV

If you want the table data in a CSV file, you can use the Export-Csv cmdlet, as shown in this example.

In this case, the result is piped into a CSV file specified on the -Path parameter. The -NoTypeInformation parameter ensures that the resulting CSV does not include object type information on the first row.

Example 4: Formatting Command Output as a Table in PowerShell

The Format-Table cmdlet can be used to format PowerShell output as a table, as shown in the example below.

The example shows how Format-Table takes the Get-Host output (a list) and formats it into a table.

You can also select specific properties to show on the output using Select-Object. For example

Instead of showing all the ten properties of the Get-Host object, the command above only picks 4.

You can also use options on Format-Table to control how your table will look like. For example, -AutoSize and -Wrap. The former dynamically adjusts the column size based on the length of the data, and the latter activates text wrapping for the table data.