As with the hashtable, the Add method may be used to add a new value to a dictionary:
$dictionary.Add("Computer1", "192.168.10.222")
If the key already exists, using Add will generate an error, as was the case with the hashtable.
In a dictionary, the Contains method behaves differently from the same method in the hashtable. When checking for the existence of a key, the ContainsKey method should be used:
if (-not $dictionary.ContainsKey("Computer2")) { $dictionary.Add("Computer2", "192.168.10.13") }
The dictionary supports the addition of elements using dot-notation:
$dictionary.Computer3 = "192.168.10.134"
The dictionary leverages PowerShell's type conversion for both the key and the value. For example, if a numeric key is used, it will be converted into a string. If an IP address is expressed as a string, it will be converted into an IPAddress object.
For example, consider the addition of the following element:
$dictionary.Add(1, 20)
In this case, the key 1 is converted into a string, and the value 20 is converted to an IPAddress. Inspecting the element afterward shows the following:
PS> $dictionary.”1”
Address : 20
AddressFamily : InterNetwork
ScopeId :
IsIPv6Multicast : False
IsIPv6LinkLocal : False
IsIPv6SiteLocal : False
IsIPv6Teredo : False
IsIPv4MappedToIPv6 : False
IPAddressToString : 20.0.0.0