PowerShell Remove Header from Output

PowerShell remove header from Output

For this article, we will use a CSV file, you can use your own, but we will be using inputFile.csv containing the following data:

Removing Header from Imported Data

We can use various ways to remove the header from the imported data before further processing in PowerShell. Let’s learn them below.

Use -HideTableHeaders Parameter

Use the -HideTableHeaders parameter to remove the header from the imported data in PowerShell.

We used Import-Csv cmdlet to import the content of the given CSV file (inputFile.csv) that we specified using -Path parameter. The Import-Csv cmdlet created the table-like custom objects & the items in rows became the property values.

This cmdlet will work on any CSV file, including those generated using the Export-Csv cmdlet. Remember that we can omit the -Path parameter and specify the file path without using this parameter name.

We stored the read data in the $inputData variable, which was piped to Format-Table cmdlet to format the output as a table. We used this cmdlet with the -HideTableHeader parameter to hide the header from the output table and stored it in the $dataWithoutHeader variable. Lastly, we used the echo command to display the data with/without a header.

Note that the -HideTableHeaders parameter of the Format-Table cmdlet does not delete/remove the header but only hides the header’s display in the output console. This is why you got enough space between the Data without Header: string and the output table without header; see the above output.

If you don’t want space in place of header, you can use Out-String cmd with trim() method.

Worth noting point is that the Format-Table cmdlet is primarily used to format data in the output console; it is not recommended to use it if you want to export the output data to a CSV file. Likewise, it doesn’t matter whether you are using Export-Csv or Set-Content cmdlets. Why?

The reason is if you use Format-Table and export the output data to a CSV file, you may get distorted or unexpected data because the Format-Table cmdlet resulted in formatted text instead of structured data, which is most suitable for a CSV format.

How to export data to a CSV file? To export the output data, you must avoid the Format-Table cmdlet and work on the structured data to have the intended output data for a CSV export. How? We will learn in the upcoming section.

Generally, the Format-Table cmdlet should be the last in a pipeline, and it is only recommended if you don’t want to export the results in a CSV file.

Use Select-Object Cmdlet with -ExpandProperty Parameter

Use the Select-Object cmdlet with the -ExpandProperty parameter to get values of one property without its header from the imported data in PowerShell.

We have learned about the Import-Csv and Select-Object previously. Here, we used the -ExpandProperty parameter to specify the property name for which we want to capture the values.

Until now, we used the Select-Object cmdlet to get values of one or all properties without a header. What if we strictly instructed to grab the values of n properties and display them on the PowerShell console? See the following script to learn how to do it.

Use Select-Object Cmdlet with the -Skip Parameter

Use the Select cmdlet with the -Skip parameter to remove the header from the imported data in PowerShell and get values for all properties.

Again, we used the Import-Csv cmdlet to import data from the CSV file we specified using the -Path parameter; we stored the imported data in the $inputData variable, which was forwarded to a multi-pipeline statement.

In this multi-pipeline statement, we used ConvertTo-Csv cmdlet to convert the .NET objects into the series of CSV strings. As we didn’t want the type information, we used the -NoTypeInformation parameter to remove the #TYPE information header from the output. This output was further piped to the ForEach statement.

The ConvertTo-Csv cmdlet is used where exporting to a file is not required due to some pending operations. And the -NoTypeInformation is the default parameter since PowerShell 6.0.

The ForEach statement iterated over the array of objects produced by the ConvertTo-Csv cmdlet and replaced the double quotes (") with an empty string ('') using the -replace operator. Here, $_ represented the current item.

Then, we used the Select-Object cmdlet with the -Skip parameter whose value was set to 1(it is the starting value for this parameter). It selected all data skipping the header, and piped it to the Set-Content cmdlet, which wrote the outputFile.csv file.

We used the Get-Content cmdlet to retrieve the content of the outputFile.csv file and the echo command to print the data with/without a header on the PowerShell console.

If you don’t want to use the ConvertTo-Csv cmdlet, then, you have to use Get-Content cmdlet to read the content of the inputFile.csv file. Othewise, your outputFile.csv would contain the content in @{FirstName=Mehvish; LastName=Ashiq; Department=Mathematics} format.

Do you want to replace the content in the primary file (inputFile.csv), then you can use the following compact command:

For this particular approach, do not forget to use () to enclose the Get-Content cmdlet so that it can read the entire inputFile.csv into memory first, which is mandatory if you are supposed to save it back to the exact file as part of the same command.

Use Select-Object with ForEach-Object Cmdlet

Use the Select-Object cmdlet with the ForEach-Object cmdlet to remove the header for n properties from the imported data.

We have already learned about the Import-Csv, Select-Object and ForEach-Object cmdlets. This approach is preferable to select the n number of properties where n is greater than 1 and less than the total number of records in the given CSV file.

Alternatively, we can use the -f operator as follows:

We imported content from the specified CSV file using the Import-Csv cmdlet, which was further piped to the ForEach-Object command to process each object. The % is an alias of the ForEach-Object cmdlet.

Within the ForEach-Object script block, we used the -f string format operator to format the output. Here, the "{0},{1}" format string defined the formatting pattern, specifying two placeholders {0} and {1} for the first name and last name.

The $_ represented the current object being processed in the pipeline; here, the FirstName and LastName properties were accessed using dot notation as $_.FirstName and $_.LastName.

Until now, we have learned how to import data from a CSV file and remove the header before further processing. What if you want to remove the header from a PowerShell command’s output?

Removing Header from PS Command’s Output

We have multiple approaches to removing a header from the output produced by the PowerShell commands. We can use any of them based on our project needs. Let’s learn a few of them below.

Use the -HideTableHeaders parameter to remove a header from the imported data in PowerShell.

Use the Select cmdlet with the -ExpandProperty parameter to get values of one property without its header in PowerShell.

Remember, Select is an alias of Select-Object, which means you can use them alternatively.

Use the ForEach Loop to get values of one property without its header in PowerShell.

Use the -f operator to get values of multiple properties without its header in PowerShell.

We have learned these cmdlets and operators while removing the header from the imported data.

That’s all about Powershell remove header from Output.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *