Home Β» PowerShell Β» Read CSV File Line by Line in PowerShell

Read CSV File Line by Line in PowerShell

PowerShell is a versatile scripting language that provides robust support for working with CSV (comma-separated) files. Its command Import-CSV is used to import CSV files and the ForEach-Object cmdlet to read a CSV file line by line in PowerShell.

Reading CSV files line by line in PowerShell is useful for processing large files like logs, and CSV or applying custom logic to each row.

In this article, we will discuss how to read CSV file line by line in PowerShell using the Import-CSV and ForEach-Object cmdlets with practical examples.

Import CSV Files in PowerShell

The Import-CSV cmdlet in PowerShell is used to import a CSV file.

# Import csv file from the specified path
$teamInfo = Import-Csv -Path D:\PS\team_info.csv

# Print the $teamInfo variable to check data available in csv file
$teamInfo

In the above PowerShell script, the Import-CSV cmdlet will read the file and store its content in the $teamInfo variable.

The output of the script after reading the CSV file is:

PS C:\> $teamInfo = Import-Csv -Path D:\PS\team_info.csv                                                                PS C:\>                                                                                                                 

PS C:\> $teamInfo                                                                                                       
Group Name Years
----- ---- -----
Dev   Tom  4
Sale  Aron 5


PS C:\>   

Read CSV files line by line

To read the CSV file line by line in PowerShell, we will use the Import-CSV cmdlet to read the content of the CSV and the ForEach-Object cmdlet to iterate through the rows.

# Import csv file from the specified path
$teamInfo = Import-Csv -Path D:\PS\team_info.csv

$teamInfo | ForEach-Object {
        # Store the individual row in $row
        $row = $_
        Write-Host "Group Name:$($row.Group), Name: $($row.Name)"
        
}

In the above PowerShell script, the Import-CSV command reads the CSV file and stores the content in the variable $teamInfo. The CSV data in a variable is pipes through the pipe operator (|) to the ForEach-Object to iterate through the rows for further processing.

The output of the above script after reading the CSV file line by line is:

PS D:\PS> # Import csv file from the specified path
$teamInfo = Import-Csv -Path D:\PS\team_info.csv

$teamInfo | ForEach-Object {
        # Store the individual row in $row
        $row = $_
        Write-Host "Group Name:$($row.Group), Name: $($row.Name)"
        
}
Group Name:Dev, Name: Tom
Group Name:Sale, Name: Aron

Filter Specific Rows from a CSV file

Let’s consider an example to read a large CSV file line by line to filter rows from a file containing a specific value in a /specific column.

# Import csv file from the specified path
$teamInfo = Import-Csv -Path D:\PS\team_info.csv

$teamInfo | ForEach-Object {
        # Check if the Group column contains "Dev" value
        if ($_.Group -eq "Dev") 
        {
            Write-Host "Dev Team Member Name: $($_.Name)"
        }
        
}

In the above PowerShell script, the Import-CSV command reads a CSV file and stores it in the $teamInfo variable. The ForEach-Object command iterates through the rows and filters rows where Group column in CSV contains the value β€˜Devβ€˜.

The output of the above script after filtering specific rows from a CSV file is:

PS D:\PS> # Import csv file from the specified path
$teamInfo = Import-Csv -Path D:\PS\team_info.csv

$teamInfo | ForEach-Object {
        
        if ($_.Group -eq "Dev") {
        Write-Host "Dev Team Member Name: $($_.Name)"
    }
        
        
}
Dev Team Member Name: Tom

PS D:\PS> 

Cool Tip: How to use the Export-CSV command to export to CSV file in PowerShell!

Conclusion

I hope the above article on how to read CSV file line by line in PowerShell using the Import-CSV and ForEach-Object cmdlets is helpful to you.

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