Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To iterate through a CSV using a ForEach loop, you will need to first read the CSV data into an array, and then use a ForEach loop to iterate through each row in the array.

Here's an example code snippet in PowerShell:

$csvData = Import-Csv -Path "C:\Users\example\Documents\data.csv"

ForEach ($row in $csvData) {
    # Access each field in the row by referencing its header name
    Write-Host "Name: $($row.Name), Age: $($row.Age), Email: $($row.Email)"
}

In this example, we're using PowerShell's Import-Csv cmdlet to read the data from a CSV file and store it in the $csvData array. Then, we use a ForEach loop to iterate through each row in the array ($row) and access its fields using their header names ($row.Name, $row.Age, and $row.Email).