Converting the Dictionary Object to Windows PowerShell Commands

Part of the VBScript to Windows PowerShell Conversion Guide

The Dictionary object (also referred to as a hash table or associative array) enables you to store data as key-item pairs. Items can be any form of data, and are stored in the array. In turn, each item is associated with a unique key. The key is used to retrieve an individual item and is usually an integer or a string, but can be anything except an array.

Trust us: it’s far more useful – and exciting – than it might sound.

To create a Dictionary object in VBScript you use code similar to this:

Set objFood = CreateObject("Scripting.Dictionary")

From there, of course, you can then use the properties and methods shown below.

Windows PowerShell uses hash tables to serve the same purpose as a Dictionary object. The hash table is created by simply assigning key/item pairs to the has variable. (The variable then becomes an instance of the .NET Framework Hashtable object.)

$food = @{"carrot" = "vegetable"; "apple" = "fruit"}

Dictionary Object Methods

Dictionary Object Properties

Top of pageTop of page