Using the VBScript Dictionary

I don’t want you to think that it is PowerShell now and forever more. VBScript isn’t going away anytime soon and I still answer plenty of VBScript questions in the forums at ScriptingAnswers.com. A topic that has come up several times in recent days is the Dictionary object. It is easy to use in VBScript and often comes in handy. Let me show you how.

First off, what is a dictionary object? It is a special type of array, technically referred to as an associative array or more familiarly as a hash table. A dictionary “entry” consists of a unique Key and an associated Item. For example, a dictionary object is like a phone book with a name and an associated phone number. Seeing the object in use will help.

To create a dictionary object is as simple as this:

Set objDict=CreateObject("Scripting.Dictionary")

You will normally want to find things in the dictionary object by keys. By default the dictionary object uses a binary compare mode which has the effect of making case-sensitive comparisons. If you will want to compare keys or search for values, you need to set the comparison before you add any items.

'default comparison is binary mode with is CaSe SenSiTive
'You can only change the CompareMode property while the Dictionary object has no keys
objDict.CompareMode=vbTextCompare

Now let’s add some items to it. I’m going to use ADSI to query the local SAMAccount database and add each user account as a dictionary key. The associated item will be their password age converted to days.

Set objNetwork=CreateObject("WScript.Network")
strComputer=objNetwork.ComputerName
Set objDomain=GetObject("WinNT://" & strComputer)
objDomain.Filter=Array("user")
 
For Each user In objDomain
   'Add items to the dictionary
   objDict.Add user.name,Int(user.PasswordAge/86400)
Next

The Add() method takes a parameter for the key and one for the item. If the key already exists you will get an error as keys must be unique. When finished objDict has members. How many?

WScript.Echo "There are " & objDict.Count & " users in the dictionary"

Pretty easy! Let’s look for a specific entry.

If objDict.Exists("Administrator") Then
    WScript.Echo "Administrator Password age (days) = " & objDict.Item("Administrator")
Else
    WScript.Echo "Failed to find Administrator account."
End If

You can use the Exists() method to see if a particular key exists. If so, the Item property will return the associated value. When I run this code I’ll get output like this:

Administrator Password age (days) = 169

What about enumerating all the keys? The Keys property returns a collection of all keys in the Dictionary so I can use a simple For Each loop to list them.

WScript.Echo "Local user accounts on " & strComputer
For Each key in objDict.Keys
    WScript.Echo " --> " & key
Next

By the way, there is no sort method.

To remove an item I’ll simply call the Remove() method.

'remove an item
If objDict.Exists("guest") Then objDict.Remove("guest")

Remember the dictionary is case sensitive by default unless you change its compare mode when you create it. Now that I’ve removed the Guest account I’ll enumerate all remaining keys and values.

WScript.Echo "Full report"
WScript.Echo "-----------"
For Each key in objDict.Keys
    'display user name, password age and date it was last changed
    WScript.Echo key & " = " & objDict.Item(key) & " days (" &_
    FormatDateTime(DateAdd("D",-objDict.Item(key),Now),vbShortDate) & ")"
Next

This snippet displays the dictionary key, it’s associated item and a value calculated from the password age. This will give me output like this:

Full report
———–
__vmware_user__ = 1 days (2/18/2009)
Administrator = 169 days (9/3/2008)
daisy = 37 days (1/13/2009)
Jeff = 185 days (8/18/2008)
lucky = 37 days (1/13/2009)
MyNewUser = 134 days (10/8/2008)

Finally, if you need to remove all items in a dictionary, perhaps because you want to reuse it, the RemoveAll() method handles this easily.

'remove all items
objDict.RemoveAll()

  You can learn more about the Dictionary object and other VBScript topics in WSH and VBScript Core: TFM. Or if you have PrimalScript 2007 or later, the VBScript help content is essentially this book. Although if you find yourself still doing a lot of VBScript I think you’ll find having a printed reference book beneficial.

 

If you’d like to try these code snippets out yourself, download the code.