Search code examples
powershell

Convert a list of key value pairs to a hashtable


What is the best way to convert a List to a Hashtable?

Say I have a list like ("Key",$value,"Key2",$value2)

What is the shortest syntax to convert it into a Hashtable?


Solution

  • Try the following

    $table = new-object System.Collections.Hashtable
    for ( $i = 0; $i -lt $list.Length; $i += 2 ) {
      $table.Add($list[$i],$list[$i+1]);
    }