Read data from CSV and create hashtable in PowerShell

Let us assume we have a CSV file with data in two columns and we need to read the data and create a hashtable for use in PowerShell. The first row in CSV is a header row with values as "Feature" and "Type".

First step is to read the CSV file using the line below
$t = Import-Csv -Path 'C:\Temp\features.csv' -Header "Feature","Type"

Now create a blank hashtable
$HashTable = @{}

Iterate through the CSV rows and add the corresponding data to hashtable as following
foreach($r in $t)
{
    Write-Host $r.Feature $r.Type
    $HashTable.Add($r.Feature,$r.Type)
}


No comments:

Post a Comment