How to retrieve CSV file headers using PowerShell?


To retrieve the CSV file headers using PowerShell, we need to use the hidden property PSObject once we use to import the CSV file. We have a CSV file stored at the C:\temp\VMTags.csv and we need to retrieve its headers. The CSV file is as below.

ABCD
ForPatching_DayApplicationOwner
AnsibleSundaySecretTagChirag

Importing the CSV file,

PS C:\> $csv = Import-Csv C:\Temp\VMTags.csv
PS C:\> $csv

For    Patching_Day Application Owner
---    ------------ ----------- -----
Ansible    Sunday    SecretTag Chirag

Example

Accessing hidden property,

PS C:\> $csv.psobject

Output

We need to use Properties to get our values. We will get all the headers here.

PS C:\> $csv.psobject.Properties | Select Name
Name
----
For
Patching_Day
Application
Owner

To get all the header values,

PS C:\> $csv.psobject.Properties | Select Value
Value
-----
Ansible
Sunday
SecretTag
Chirag

Updated on: 06-Apr-2021

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements