Table of Contents
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:
12345678 FirstName,LastName,DepartmentShruti,Mehta,Computer ScienceMehvish,Ashiq,MathematicsJohn,Christopher,PsychologySaira,Daniel,HumanitiesGurmeet,Kaur,Arts
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.
1 2 3 4 5 6 |
$inputData = Import-Csv -Path "E:/Test/inputFile.csv" $dataWithoutHeader = $inputData | Format-Table -HideTableHeaders echo "Data with Header:", $inputData echo "`nData without Header:", $dataWithoutHeader |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Data with Header: FirstName LastName Department --------- -------- ---------- Shruti Mehta Computer Science Mehvish Ashiq Mathematics John Christopher Psychology Saira Daniel Humanities Gurmeet Kaur Arts Data without Header: Shruti Mehta Computer Science Mehvish Ashiq Mathematics John Christopher Psychology Saira Daniel Humanities Gurmeet Kaur Arts |
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.
1 2 3 4 5 |
$inputData = Import-Csv -Path "E:/Test/inputFile.csv" $dataWithoutHeader = ($inputData | Format-Table -HideTableHeaders| Out-String).trim() echo "Data without Header:", $dataWithoutHeader |
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.
1 2 3 |
Import-Csv "E:/Test/inputFile.csv" | Select-Object -ExpandProperty FirstName |
1 2 3 4 5 6 7 |
Shruti Mehvish John Saira Gurmeet |
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.
Further reading:
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.
1 2 3 4 5 6 7 8 9 10 11 |
$inputData = Import-Csv -Path "E:/Test/inputFile.csv" $inputData | ConvertTo-Csv -NoTypeInformation | ForEach{ $_ -replace '"' ,''} | Select-Object -Skip 1 | Set-Content "E:/Test/outputFile.csv" echo "Data with Header:", $inputData $outputData = Get-Content "E:/Test/outputFile.csv" echo "`nData without Header:", $outputData |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Data with Header: FirstName LastName Department --------- -------- ---------- Shruti Mehta Computer Science Mehvish Ashiq Mathematics John Christopher Psychology Saira Daniel Humanities Gurmeet Kaur Arts Data without Header: Shruti,Mehta,Computer Science Mehvish,Ashiq,Mathematics John,Christopher,Psychology Saira,Daniel,Humanities Gurmeet,Kaur,Arts |
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.
1 2 3 4 5 6 7 8 9 |
$inputData = Get-Content "E:/Test/inputFile.csv" $inputData | Select-Object -Skip 1 | Set-Content "E:/Test/outputFile.csv" echo "Data with Header:", $inputData $outputData = Get-Content "E:/Test/outputFile.csv" echo "`nData without Header:", $outputData |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Data with Header: FirstName,LastName,Department Shruti,Mehta,Computer Science Mehvish,Ashiq,Mathematics John,Christopher,Psychology Saira,Daniel,Humanities Gurmeet,Kaur,Arts Data without Header: Shruti,Mehta,Computer Science Mehvish,Ashiq,Mathematics John,Christopher,Psychology Saira,Daniel,Humanities Gurmeet,Kaur,Arts |
Do you want to replace the content in the primary file (inputFile.csv
), then you can use the following compact command:
1 2 3 4 5 |
(Get-Content E:/Test/inputFile.csv)| Select-Object -Skip 1| Set-Content "E:/Test/inputFile.csv" |
1 2 3 4 5 6 7 |
Shruti,Mehta,Computer Science Mehvish,Ashiq,Mathematics John,Christopher,Psychology Saira,Daniel,Humanities Gurmeet,Kaur,Arts |
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.
1 2 3 4 5 |
Import-Csv "E:/Test/inputFile.csv" | Select-Object FirstName, LastName | ForEach-Object {$_.FirstName + "," + $_.LastName} |
1 2 3 4 5 6 7 |
Shruti,Mehta Mehvish,Ashiq John,Christopher Saira,Daniel Gurmeet,Kaur |
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:
1 2 3 4 |
Import-Csv "E:/Test/inputFile.csv" | %{ "{0},{1}" -f $_.FirstName,$_.LastName } |
1 2 3 4 5 6 7 |
Shruti,Mehta Mehvish,Ashiq John,Christopher Saira,Daniel Gurmeet,Kaur |
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.
1 2 3 |
Get-Alias | Format-Table -HideTableHeaders |
1 2 3 4 5 6 7 8 9 10 11 12 |
Alias % -> ForEach-Object Alias ? -> Where-Object Alias ac -> Add-Content Alias asnp -> Add-PSSnapin Alias cat -> Get-Content Alias cd -> Set-Location Alias CFS -> ConvertFrom-String 3.1.0.0 Microsoft.PowerShell.Utility ... ... ... ... ... ... ... ... ... ... ... ... |
Use the Select
cmdlet with the -ExpandProperty
parameter to get values of one property without its header in PowerShell.
1 2 3 |
Get-Alias | Select -ExpandProperty Name |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
% ? ac asnp cat cd CFS chdir ... ... ... |
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.
1 2 3 |
Get-Alias | ForEach {$_.Name} |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
% ? ac asnp cat cd CFS chdir ... ... ... |
Use the -f
operator to get values of multiple properties without its header in PowerShell.
1 2 3 |
Get-Alias | %{ "{0},{1}" -f $_.CommandType,$_.Definition } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Alias,ForEach-Object Alias,Where-Object Alias,Add-Content Alias,Add-PSSnapIn Alias,Get-Content Alias,Set-Location Alias,ConvertFrom-String Alias,Set-Location Alias,Clear-Content ... , ... ... , ... ... , ... |
We have learned these cmdlets and operators while removing the header from the imported data.
That’s all about Powershell remove header from Output.