Import CSV Files Into Array in PowerShell

This article discusses how to load a CSV file into a PowerShell array using two methods:

  • Method 1: Using Import-Csv cmdlet, and
  • Method 2: Reading CSV data with Get-Content and then processing each row.

We will run our code on PowerShell 7.3.2 using a “employees – list.csv” file with the following contents (opened with Notepad):

ID,Position,Salary,Joining Date
2001,Junior web developer,5600,8/27/2020
2006,Accountant,7200,7/22/2019
3014,Senior Data Scientist,7800,5/1/2013
1946,HR Manager,6200,9/9/2021
1947,Managing Director,22000,9/24/2009
1901,Procurement officer 2,6100,12/8/2010
1902,HR,5700,12/8/2010
2008,Procurement officer,6100,2/8/2011

The first row contains the fields, and the other rows hold the records. The content above shows that the “employees – list.csv” is a comma-delimited file. Note that you can use other characters as delimiters – for example, semi-colons or hash symbols.

Let’s now discuss how to read CSV files into PowerShell arrays.

Method 1: Using Import-Csv Cmdlet

Import-Csv cmdlet loads CSV data into a table-like custom object. Each column in the CSV becomes a property of the custom object, and the row values become the property values.

The cmdlet has the following syntax (showing three relevant options only):

Import-Csv -Path <path_to_csv_file> -Delimiter <value_separator> -Header <customer_headers>

Where -Path is the path to our CSV file, -Delimiter is the values separator (if a value is not passed to this option, a comma is used as delimiter), -Header contains the custom headers (if this option is not supplied the first row of the CSV is used as the headers).

Let’s use the cmdlet to load the CSV file described earlier.

You can also load specific fields from the CSV file by piping the output of Import-Csv to the Select-Object cmdlet. Here is an example showing how to load ID and Position columns only.

Accessing values of the PowerShell array

The output of the Import-Csv cmdlet can be accessed like a typical PowerShell array.

Note: PowerShell uses zero-based indexing; that is, the first element of an array is given index 0, the second element is at index 1, and so on.

Here are some examples of how to access the values.

Method 2: Using Get-Content and ForEach

This method brings a different blend of control in processing the loaded data. The Get-Content cmdlet gets the data on the CSV file then we loop through the rows using the ForEach command. Here is an example.

Output (truncated):

Then you can access the array values as we did in Method 1

Output:

Accountant
720
7/22/2019
3014
Senior Data Scientist

Alternatively, we can load the data into a hash table of array values, as shown below.

Output:

Now, you can access the data on $hash_data like any other PowerShell hash table and/or array. For example,

Output (truncated):

8/27/2020
7/22/2019
…

We can also pack the CSV data into an array, each element being a hash table of row values.

Output (truncated):

Then you can access the row values through indexing. For example, $arr_data[0] will fetch the hash table for the values in the first row and $arr_data[1..3].Id gets the Id values for rows 2 through 4.

Conclusion

This guide discussed two PowerShell cmdlet tools that can load a CSV file into an array. The first method uses the Import-Csv to read CSV into a table-like custom object made of column names as properties and row values as property values. The values of the custom object can then be fetched through indexing.

The second method uses the Get-Content cmdlet. In this method, the CSV file is read using the cmdlet then the records are fetched and processed through looping.