PowerShell: How to Use Import-Csv with No Headers


When using the Import-Csv cmdlet in PowerShell to import a CSV file, the first line in the file is used as the header by default.

However, occasionally you may want to import a CSV file that has no header row.

You can use the following methods to import a CSV file with no header row and specify the header names that should be used:

Method 1: Use Import-Csv with Custom Header Names

Import-Csv $my_file -Header Team, Points, Assists

This particular example imports the CSV file specified at the path in the $my_file variable and specifies that the header names should be Team, Points and Assists.

Method 2: Use Import-Csv with Numbered Header Names

$headers = 1..3
Import-Csv $my_file -Header $headers

This particular example imports the CSV file specified at the path in the $my_file variable and specifies that the header names should simply be the numbers ranging from 1 to 3.

This method is particularly useful when you have a lot of columns and you don’t want to individually specify each column name.

For example, you could use $headers = 1..500 to use the integers ranging from 1 to 500 as the column names for a file that has 500 columns.

The following examples show how to use each method in practice.

Example 1: Use Import-Csv with Custom Header Names

Suppose we have a CSV file saved in the following location:

  • c:\users\bobbi\data_no_header.csv

We can use the Get-Content cmdlet to view the content of this file:

The file contains three columns that show the team name, points and assists for various basketball players.

Notice that the file does not have a header row.

We can use the following syntax to import this CSV file and specify names to be used as the headers:

Import-Csv $my_file -Header Team, Points, Assists

The following screenshot shows how to use this syntax in practice:

PowerShell Import-Csv no header

We can see that we’re able to successfully import this CSV file and specify the header names that should be used.

Example 2: Use Import-Csv with Numbered Header Names

Once again suppose we have a CSV file saved in the following location:

  • c:\users\bobbi\data_no_header.csv

We can use the Get-Content cmdlet to view the content of this file:

Notice that the file does not have a header row.

We can use the following syntax to import this CSV file and specify that the numbers 1, 2 and 3 should be used as the header names:

$headers = 1..3
Import-Csv $my_file -Header $headers

The following screenshot shows how to use this syntax in practice:

PowerShell import-csv use numbers as column names

We can see that we’re able to successfully import this CSV file and specify that the numbers 1, 2 and 3 should be used as the header names.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Use Export-Csv with No Headers
PowerShell: How to Use Import-Csv and Foreach
PowerShell: How to Merge Multiple CSV Files into One

Leave a Reply