How to Import CSV to Array in PowerShell?

Recently, I got a requirement to import data from a CSV (Comma Separated Values) file into an array in PowerShell for further processing. In this tutorial, I will show you how to import CSV data into an array in PowerShell using different methods with examples.

To import CSV data into an array in PowerShell, use the Import-Csv cmdlet followed by the file path, which creates an array of objects from the CSV file. For example, $array = Import-Csv -Path ‘file.csv’ reads ‘file.csv’ and stores the data in $array. Each row in the CSV becomes an object in the array, with properties corresponding to the column headers.

Import CSV to Array in PowerShell using Import-Csv Cmdlet

A CSV file is a plain text file that contains data separated by commas. Each line in a CSV file typically represents a data record, and each record consists of one or more fields separated by commas. CSV files are widely used for data exchange because they are easy for many applications to create, read, and process.

There are different methods to import CSV to an array in PowerShell.

Understanding the Import-Csv Cmdlet

PowerShell offers a built-in cmdlet called Import-Csv that is designed specifically for reading CSV files and converting them into objects. According to the Microsoft documentation, the Import-Csv cmdlet creates table-like custom objects from the items in CSV files, with each column becoming a property of the custom object.

The simplest way to import CSV data into PowerShell is by using the Import-Csv cmdlet. This cmdlet reads the CSV file and converts it into an array of objects, where each object represents a row in the CSV file.

Here’s a basic example:

$data = Import-Csv -Path "C:\path\to\yourfile.csv"

In this example, $data is an array where each element is a custom object with properties corresponding to the column headers in the CSV file.

After importing the CSV data, you can process each record using a ForEach loop. Here’s an example that displays each record:

$data = Import-Csv -Path "C:\path\to\yourfile.csv"
foreach ($record in $data) {
    Write-Host "Name: $($record.Name), Age: $($record.Age)"
}

Assuming the CSV file has columns named “Name” and “Age,” this script will print out the name and age of each record.

If your CSV file doesn’t contain headers, you can specify them using the -Header parameter:

$headers = "Name", "Age", "Occupation"
$data = Import-Csv -Path "C:\path\to\yourfile.csv" -Header $headers

Now, $data will use the provided headers to create objects.

Some CSV files may use different delimiters, such as semicolons or tabs. You can specify the delimiter with the -Delimiter parameter:

$data = Import-Csv -Path "C:\path\to\yourfile.csv" -Delimiter ";"

An ArrayList is a dynamic array that can grow or shrink as needed. To import CSV data into an ArrayList, you can use the Import-Csv cmdlet in combination with a ForEach loop:

[array]$data = @()
Import-Csv -Path "C:\path\to\yourfile.csv" | ForEach-Object {
    $data += $_
}

This script reads the CSV file and adds each record to the $data array.

Import CSV to Array in PowerShell – Real Example

Let’s say you have a CSV file named users.csv with the following content:

Id,Name,Email
1,John Doe,johndoe@example.com
2,Jane Smith,janesmith@example.com
3,Emily Jones,emilyjones@example.com

To import this CSV into an array in PowerShell and display each user’s details, you would use the following script:

# Import the CSV file into an array
$usersArray = Import-Csv -Path "C:\path\to\users.csv"

# Loop through the array and display user details
foreach ($user in $usersArray) {
    Write-Host "User ID: $($user.Id)"
    Write-Host "Name: $($user.Name)"
    Write-Host "Email: $($user.Email)"
    Write-Host "-------------------"
}

Running this script would output:

User ID: 1
Name: John Doe
Email: johndoe@example.com
-------------------
User ID: 2
Name: Jane Smith
Email: janesmith@example.com
-------------------
User ID: 3
Name: Emily Jones
Email: emilyjones@example.com
-------------------

Conclusion

Importing CSV data into an array in PowerShell can be achieved easily using the Import-Csv cmdlet. In this PowerShell tutorial, I have explained how to Import CSV to Array in PowerShell using Import-Csv Cmdlet.

You may also like: