Home ยป How to Get Columns for a CSV File in PowerShell

How to Get Columns for a CSV File in PowerShell

To get columns (headers) for a CSV file using PowerShell, follow the below steps:

  • Use the Import-Csv cmdlet to read and import the csv file content. Store the content in the variable for further manipulation.
$csv = Import-Csv -Path D:\PS\team_info.csv
  • Access the headers of a CSV file using the `PSObject.Properties.Name` property of the imported CSV data variable.
 $csv[0].psobject.Properties.Name

The above PowerShell script imports the CSV file and retrieves the column names (header) for a CSV file.

The output of the above PowerShell script to retrieve CSV headers is:

PowerShell Get Column Names of CSV File
PowerShell Get Column Names of CSV File

Cool Tip: How to import csv file with no headers in PowerShell!

Conclusion

I hope the above article on how to get column names (headers) of a CSV file using the PowerShell Import-Csv cmdlet with PSObject.Properties.Name property is helpful to you.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.