Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

#12 PowerShell Learning: Hash Tables in Windows PowerShell

August 14, 2023
in Blog, PowerShell Learning
0
ADVERTISEMENT

Table of Contents

What is a Hash Table in PowerShell?

A hash table, also known as a dictionary or associative array, is a compact data structure that stores one or more key/value pairs. For example, a hash table can contain a series of Computer names and IP addresses where the Computer Names are the keys and the IP Addresses are the values, or vice versa.

In Windows PowerShell, each hash table is a System.Collections.Hashtable object.  You can use the properties and methods of Hashtable objects in Windows PowerShell.

The keys and value in hash tables are also .NET objects. They are most often strings or integers, but they can have any object type. You can also create nested hash tables, in which the value of a key is another hash table.

Hash tables are frequently used because they are very efficient for finding and retrieving data. You can use hash tables to store lists and to create calculated properties in Windows PowerShell. Windows PowerShell gives a ConvertFrom-StringData cmdlet, which converts strings to a hash table.

The syntax of a hash table is as follows:

@{ <name> = <value>; <name> = <value> …}

Creating Hash Tables

To create a hash table, follow these guidelines:

  • Begin the hash table with an at sign @.
  • Enclose the hash table in curly braces {}.
  • Enter one or more key/value pairs for the content of the hash table.
  • Use an equal sign = to separate each key from its value.
  • Use a semicolon ; or a line break to separate the key/value pairs.
  • The Keys that contain spaces must be enclosed in quotation marks. The Values must be valid Windows PowerShell expressions. The Strings must appear in quotation marks, even if they do not include spaces.
  • To manage the hash table, save it in a variable.
  • When assigning an ordered hash table to a variable, place the [ordered] attribute before the @ symbol. If you place it before the variable name, the command fails.

For example, to create an empty hash table in the value of $hash, use $hash = @{} command.

PS C:\> $hashtable = @{}
PS C:\> $hashtable
PS C:\>

You can also add keys and values to a hash table when you create it. For example, the following statement creates a hash table with two keys.

PS C:\> $hashtable = @{"ComputerName"="DC1";"IP Address"="192.168.10.10"}
PS C:\> $hashtable

Name                           Value
----                           -----
ComputerName                   DC1
IP Address                     192.168.10.10

Displaying Hash Table

To display a hash table that is saved in a variable, type the variable name. By default, a hash tables is displayed as a table with one column for keys and one for values.

PS C:\> $hashtable

Name                           Value
----                           -----
ComputerName                   DC1
IP Address                     192.168.10.10

To get the members of a hash table, pipe the hash table to Get-Member cmdlet.

PS C:\> $hashtable | Get-Member -MemberType Properties

   TypeName: System.Collections.Hashtable

Name           MemberType Definition
----           ---------- ----------
Count          Property   int Count {get;}
IsFixedSize    Property   bool IsFixedSize {get;}
IsReadOnly     Property   bool IsReadOnly {get;}
IsSynchronized Property   bool IsSynchronized {get;}
Keys           Property   System.Collections.ICollection Keys {get;}
SyncRoot       Property   System.Object SyncRoot {get;}
Values         Property   System.Collections.ICollection Values {get;}

Hash tables have Count, Keys and Values properties. You can use dot notation to display all count of key/value pairs or to display all of the keys or all of the values.

PS C:\> $hashtable.count
2
PS C:\> $hashtable.keys
ComputerName
IP Address
PS C:\> $hashtable.values
DC1
192.168.10.10

Each key name is also a property of the hash table, and its value is the value of the key-name property. Use the following format to display the property values.

PS C:\> $hashtable.ComputerName
DC1
PS C:\> $hashtable."IP Address"
192.168.10.10

Add or Remove the Keys and Values

To add keys and values to a hash table, use the following command format.

$hashtable["<key>"] = "<value>"

For example, to add a DomainName key with a value of bonguides.com to the hash table, use the following command:

PS C:\> $hashtable.add("DomainName" , "bonguides.com")
PS C:\> $hashtable

Name                           Value
----                           -----
IP Address                     192.168.10.10
DomainName                     bonguides.com
ComputerName                   DC1

You can add keys and values to a hash table by using the addition operator +. For example, the following statement adds an OperatingSystem key with a value of Server 2012 R2 to the hash table in the $hashtable variable.

PS C:\> $hashtable = $hashtable + @{OperatingSystem = "Server 2012 R2"}
PS C:\> $hashtable

Name                           Value
----                           -----
IP Address                     192.168.10.10
ComputerName                   DC1
OperatingSystem                Server 2012 R2
DomainName                     bonguides.com

You cannot use a subtraction operator (-) to remove a key/value pair from a hash table, but you can use the Remove method of the Hashtable object. The Remove method takes the key as its value.

For example, to remove the DomainName=bonguides.com key/value pair from the hash table in the value of the $hashtable variable, use the following command:

PS C:\> $hashtable.Remove("DomainName")
PS C:\> $hashtable

Name                           Value
----                           -----
IP Address                     192.168.10.10
ComputerName                   DC1
OperatingSystem                Server 2012 R2

In the same way you can use all of the properties and methods of Hashtable objects in Windows PowerShell.

PS C:\> $hashtable | Get-Member

   TypeName: System.Collections.Hashtable

Name              MemberType            Definition
----              ----------            ----------
Add               Method                void Add(System.Object key, System.Object value), void IDictionary.Add(Syste...
Clear             Method                void Clear(), void IDictionary.Clear()
Clone             Method                System.Object Clone(), System.Object ICloneable.Clone()
Contains          Method                bool Contains(System.Object key), bool IDictionary.Contains(System.Object key)
ContainsKey       Method                bool ContainsKey(System.Object key)
ContainsValue     Method                bool ContainsValue(System.Object value)
CopyTo            Method                void CopyTo(array array, int arrayIndex), void ICollection.CopyTo(array arra...
Equals            Method                bool Equals(System.Object obj)
GetEnumerator     Method                System.Collections.IDictionaryEnumerator GetEnumerator(), System.Collections...
GetHashCode       Method                int GetHashCode()
GetObjectData     Method                void GetObjectData(System.Runtime.Serialization.SerializationInfo info, Syst...
GetType           Method                type GetType()
OnDeserialization Method                void OnDeserialization(System.Object sender), void IDeserializationCallback....
Remove            Method                void Remove(System.Object key), void IDictionary.Remove(System.Object key)
ToString          Method                string ToString()
Item              ParameterizedProperty System.Object Item(System.Object key) {get;set;}
Count             Property              int Count {get;}
IsFixedSize       Property              bool IsFixedSize {get;}
IsReadOnly        Property              bool IsReadOnly {get;}
IsSynchronized    Property              bool IsSynchronized {get;}
Keys              Property              System.Collections.ICollection Keys {get;}
SyncRoot          Property              System.Object SyncRoot {get;}
Values            Property              System.Collections.ICollection Values {get;}
5/5 - (1 vote)
Previous Post

#11 PowerShell Learning: Arrays in Windows PowerShell

Next Post

#13 PowerShell Learning: PowerShell Loops

Related Posts

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 2024

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory