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
).
Asked: 2021-09-01 11:00:00 +0000
Seen: 11 times
Last updated: Mar 26 '22