PowerShell Hash Tables | The AEMS Flogger (2024)

A hash table is an array of key/value pairs, along with accompanying functions for
readily accessing or modifying values by way of their corresponding keys.

Here is the PowerShell syntax for initializing an empty hash table object:

$myHashTable = @{}

We add elements to the hash table quite easily via several different mechanisms.

$key = "key 1"$value = "value 1"$myHashTable.add($key,$value)$myHashTable.add("key 2", "value 2")

Here is what the resulting hash table looks like:

PS C:\> $myHashTableName Value ---- ----- key 2 value 2 key 1 value 1

Alternatively, we could have populated the hash table at the time that we created it via the following syntax:

$myHashTable2 = @{ 100 = "One Hundred" 1000 = "One Thousand" 10000 = "Ten Thousand" }

It should be noted that order is not necessarily preserved in a hash table:

PS C:\> $myHashTable2Name Value ---- ----- 1000 One Thousand 10000 Ten Thousand 100 One Hundred

There is, however, a syntax for constructing a hash table of type [ordered] if that is needed:

PS C:\> $myHashTable2 = [ordered]@{ 100 = "One Hundred" 1000 = "One Thousand" 10000 = "Ten Thousand" }PS C:\> $myHashTable2Name Value ---- ----- 100 One Hundred 1000 One Thousand 10000 Ten Thousand

Accessing the values of hash table entries by means of the keys is pretty straightforward, and multiple syntaxes are provided for doing so.

PS C:\> $myHashTable["key 1"]value 1PS C:\> $myHashTable."key 2"value 2PS C:\> $myHashTable2.1000One Thousand

I should note here, however, that the [] syntax changes behavior when the hash table is defined as an ordered hash table (as in the way in which I have redefined $myHashTable2 in these examples). In that case, the arguments in square brackets are passed as an array index rather than a key.

PS C:\> $myHashTable2[100]PS C:\> $myHashTable2[0]One Hundred

blah

Keep in mind that key names are simply strings, and since PowerShell strings by default use UTF-16 encoding, we are squarely in the world of Unicode, which can, in turn, include Emoji.

PS C:> $myHashTable = @{"💩" = "Poo Emoji" "🎼" = "Treble Clef""⏰" = "Clock""😁" = "Laughing Emoji"}PS C:> $myHashTable."🎼"Treble ClefPS C:> $myHashTable."💩"Poo Emoji

I don’t know how useful this particularly is, but it amuses me. It is somewhat reminiscent of the ability to use Unicode strings to define variable names in Apple’s programming language, Swift.

Here is a more practical example in which the hash table is used literally as a dictionary lookup table, translating Korean words (rendered in Hangul) to English.

PS C:\> $myHashTable = @{ 가다 = "to go" 웃다 = "to come" 먹다 = "to eat"}PS C:\> $myHashTable.가다to go

Creating Calculated Properties

When using Select-Object, Format-List, or Format-Table, it is possible to use hash tables to dynamically rename attributes or to calculate synthetically-generated attributeson the fly. For example, running the Get-ChildItem command to examine filesystem objects results in the permissions of each object being returned in an attribute called “Mode”. We can use a hash table to rewrite this attribute name on the fly as something more descriptive.

PS C:\> Get-ChildItem | where {$_.Name -like "Users"} | select Mode,Name | ft -AutoSizeMode Name ---- ---- d-r-- UsersPS C:\> $property = @{ name = "Permissions" expression = {$_.Mode} }PS C:\> Get-ChildItem | where {$_.Name -like "Users"} | select $property,Name | ft -AutoSizePermissions Name ----------- ---- d-r-- Users

Of course, this particular sort of application of hash tables in PowerShell is more commonly seen with the hash table defined in-line. However, that approach somewhat degrades readability of the script.

PS C:\> Get-ChildItem | where {$_.Name -like "Users"} | select @{name = "Permissions"; expression = {$_.Mode}},Name | ft -AutoSizePermissions Name ----------- ---- d-r-- Users

We’ve shown how to rename a property on the fly, but how about dynamically generating calculated attributes? For example, when we run Get-ChildItem on files, the size is returned, in bytes, in an attribute called “Length”.

PS C:\Windows\System32> Get-ChildItem win*.exe | ft Name,Length -AutoSizeName Length---- ------wininit.exe 129024winload.exe 634272winlogon.exe 455680winresume.exe 546656winrs.exe 46080winrshost.exe 23040winver.exe 80384

What if we instead want the size to be shown in kilobytes, with the name of the property modified appropriately to reflect this?

PS C:\Windows\System32> $size = @{name = "Size (KB)"expression = {($_.Length)/1KB}}PS C:\Windows\System32> Get-ChildItem win*.exe | ft Name,$size -AutoSizeName Size (KB)---- ---------wininit.exe 126winload.exe 619.40625winlogon.exe 445winresume.exe 533.84375winrs.exe 45winrshost.exe 22.5winver.exe 78.5

It is possible to pass collections of parameters to PowerShell cmdlets in the form of hash tables. This feature, introduced in PowerShell v2, is called “splatting.”

PS C:> $myColors = @{ForegroundColor = "Cyan"BackgroundColor = "Black"}PS C:> Write-Host "Hello" @myColorsHello

You can mix in “splatted” parameters with parameters passed via normal means, as well as passing mulitple hash tables, all provided that none the parameters passed conflict.

PS C:> $sep = @{ Separator = "<---->" }PS C:> Write-Host "Thing 1","Thing 2","Thing 3" @sep @myColors -NoNewline ; Write-Host "!"Thing 1<---->Thing 2<---->Thing 3!

blah

Note that this list is by no means complete but rather reflects what I see in the tenant that I administer. It even includes SKUs that have since been deprecated and replaced by other SKUs. This hash table includes both SKUs and services, and the entries are presented in no particular order.

$names = @{ "STANDARDWOFFPACK_STUDENT" = "Office 365 A1 for students" "STANDARDWOFFPACK_IW_STUDENT"="Office 365 A1 Plus for students" "PROJECTONLINE_PLAN_1_FACULTY"="Project Online Premium without Project Client for faculty" "EXCHANGEENTERPRISE_FACULTY"="Exchange Online (Plan 2) for faculty" "PROJECTONLINE_PLAN_2_FACULTY"="Project Online with Project Pro for Office 365 for Faculty" "STANDARDWOFFPACK"="Microsoft Office 365 Plan E2" "OFFICESUBSCRIPTION_FACULTY"="Office 365 ProPlus for faculty" "OFFICESUBSCRIPTION_STUDENT"="Office 365 ProPlus for students" "STANDARDWOFFPACK_FACULTY"="Office 365 A1 for faculty" "ATP_ENTERPRISE_FACULTY" = "Exchange Online Advanced Threat Protection for Faculty" "RMS_S_ENTERPRISE"="Azure Rights Management" "OFFICE_FORMS_PLAN_2"="Microsoft Forms (Plan 2)" "PROJECTWORKMANAGEMENT"="Microsoft Planner" "SWAY"="Sway" "INTUNE_O365"="Mobile Device Management for Office 365" "YAMMER_EDU"="Yammer for Academic" "SHAREPOINTWAC_EDU" = "Office Online for Education" "MCOSTANDARD" = "Skype for Business Online (Plan 2)" "SHAREPOINTSTANDARD_EDU" = "SharePoint Plan 1 for EDU" "EXCHANGE_S_STANDARD" = "Exchange Online (Plan 1)" "OFFICESUBSCRIPTION" = "Office 365 ProPlus" "EXCHANGE_S_FOUNDATION" = "Exchange Foundation (?)" "SHAREPOINT_PROJECT_EDU" = "Project Online Service for Education" "SHAREPOINTENTERPRISE_EDU" = "SharePoint Plan 2 for EDU" "EXCHANGE_S_ENTERPRISE" = "Exchange Online (Plan 2)" "PROJECT_CLIENT_SUBSCRIPTION" = "Project Online Desktop Client" "ONEDRIVESTANDARD" = "OneDrive for Business (Plan 1)" "FLOW_O365_P2" = "Flow for Office 365" "POWERAPPS_O365_P2" = "PowerApps for Office 365" "Deskless" = "Microsoft StaffHub" "TEAMS1" = "Microsoft Teams" "STREAM_O365_E3" = "Microsoft Stream for O365 E3 SKU" "EMS" = "Enterprise Mobility + Security E3" "RMS_S_PREMIUM" = "Azure Information Protection Plan 1" "INTUNE_A" = "Intune A Direct" "AAD_PREMIUM" = "Azure Active Directory Premium Plan 1" "MFA_PREMIUM" = "Azure Multi-Factor Authentication" "POWER_BI_STANDARD_FACULTY" = "Power BI (free) for faculty" "BI_AZURE_P0" = "Power BI (free)" "ATP_ENTERPRISE" = "Advanced Threat Protection" "MCOMEETADV_FACULTY" = "Skype for Business PSTN Conferencing for faculty" "MCOMEETADV" = "Skype for Business PSTN Conferencing" "SCHOOL_DATA_SYNC_P1" = "School Data Sync (Plan 1)" "FORMS_PLAN_E1" = "Microsoft Forms (Plan E1)" "PROJECTPREMIUM_FACULTY" = "Project Online Premium for faculty" "PROJECTESSENTIALS_FACULTY" = "Project Online Essentials for faculty" "PROJECT_ESSENTIALS" = "Project Online Essentials" "BPOS_S_TODO_2"="To-Do (Plan 2)" "AAD_BASIC_EDU"="Azure Active Directory Basic for Edu" "ADALLOM_S_DISCOVERY" = "Cloud App Security Discovery" "POWER_BI_PRO_FACULTY" = "Power BI Pro for faculty" "PROJECTPROFESSIONAL_FACULTY" = "Project Online Professional for faculty" }

Let’s say that we want to use this hash table to show the friendly names for all of the SKUs returned by Get-MsolAccountSku:

PS C:> $skus=Get-MsolAccountSkuPS C:> foreach ($sku in $skus){ $skupart = $sku.SkuPartNumber Write-Host ("{0,-28} {1,-40}" -f $skupart,$names.Item($skupart)) }PROJECTESSENTIALS_FACULTY Project Online Essentials for faculty MCOMEETADV_FACULTY Skype for Business PSTN Conferencing for facultyATP_ENTERPRISE_FACULTY Exchange Online Advanced Threat Protection for FacultyPOWER_BI_STANDARD_FACULTY Power BI (free) for faculty STANDARDWOFFPACK_STUDENT Office 365 A1 for students POWER_BI_PRO_FACULTY Power BI Pro for faculty STANDARDWOFFPACK_IW_STUDENT Office 365 A1 Plus for students EXCHANGEENTERPRISE_FACULTY Exchange Online (Plan 2) for faculty PROJECTPROFESSIONAL_FACULTY Project Online Professional for faculty EMS Enterprise Mobility + Security E3 OFFICESUBSCRIPTION_FACULTY Office 365 ProPlus for faculty OFFICESUBSCRIPTION_STUDENT Office 365 ProPlus for students STANDARDWOFFPACK_FACULTY Office 365 A1 for faculty PROJECTPREMIUM_FACULTY Project Online Premium for faculty

Suppose that we no want to take a look at all of the services under a specific SKU, say the one for EMS:

PS C:\Working\Scripts\glenmartin\lice> foreach ($service in $skus[9].ServiceStatus) { $ServName = $names.Item($service.ServicePlan.ServiceName) Write-Host ("{0} {1,-25} {2,-40} {3,-15}" -f "`t",$service.ServicePlan.ServiceName,$ServName,$service.ProvisioningStatus) } EXCHANGE_S_FOUNDATION Exchange Foundation (?) Success ADALLOM_S_DISCOVERY Cloud App Security Discovery Success RMS_S_PREMIUM Azure Information Protection Plan 1 Success INTUNE_A Intune A Direct Success RMS_S_ENTERPRISE Azure Rights Management Success AAD_PREMIUM Azure Active Directory Premium Plan 1 Success MFA_PREMIUM Azure Multi-Factor Authentication Success

(Note that I have a question mark in the Exchange Foundation service name since Microsoft does not document this one at all, nor does this service get listed in the web interface.)

PowerShell Hash Tables | The AEMS Flogger (2024)

FAQs

How to retrieve value from Hashtable PowerShell? ›

To retrieve a value from a Hashtable using a key in PowerShell, you can use the square bracket notation. For example, if your Hashtable is named $hash and you want to retrieve the value associated with the key “key1”, you can use $hash[“key1”]. This will return the corresponding value.

What is @{} in PowerShell? ›

@{} in PowerShell defines a hashtable, a data structure for mapping unique keys to values (in other languages this data structure is called "dictionary" or "associative array").

What is the format of hash table in PowerShell? ›

The syntax of a hashtable is as follows: @{ <name> = <value>; [<name> = <value> ] ...} The syntax of an ordered dictionary is as follows: [ordered]@{ <name> = <value>; [<name> = <value> ] ...}

How to add values to hashtable in PowerShell? ›

You can also use the Hashtable method called Add() to add the value. The format is as below. To remove the Key-value from the Hashtable, you need to use the Remove(Key) method. You cannot remove the hashtable entry with the values.

How to retrieve value stored in hash table? ›

To retrieve the value associated with a key, apply the hash function to the key to determine the index. If the index contains a value, compare the stored key with the given key to ensure a match. If the keys match, return the corresponding value.

How to get hashtable value? ›

get() method is used to retrieve the value of the passed key from the HashTable .

How to print a hash table in PowerShell? ›

Printing hashtable: To print /display a hashtable, just type the variable name in which hashtable is saved. The above command displays a table with two columns, one for keys and the other one for the associated values for the keys.

How to get value from variable in PowerShell? ›

The Get-Variable cmdlet gets the PowerShell variables in the current console. You can retrieve just the values of the variables by specifying the ValueOnly parameter, and you can filter the variables returned by name.

How do you sort a hashtable by key in PowerShell? ›

You have a hashtable of keys and values, and want to get the list of values that result from sorting the keys in order. To sort a hashtable, use the GetEnumerator() method on the hashtable to gain access to its individual elements.

What is the difference between array and Hashtable in PowerShell? ›

Like many other scripting and programming languages, Windows PowerShell allows you to work with arrays and hash tables. An array is a collection of values that can be stored in a single object. A hash table is also known as an associative array and is a dictionary that stores a set of key-value pairs.

How to hash using PowerShell? ›

PowerShell does not provide a cmdlet to compute the hash of a string. However, you can write a string to a stream and use the InputStream parameter of Get-FileHash to get the hash value.

How to convert string to hashtable in PowerShell? ›

The ConvertFrom-StringData cmdlet converts a string that contains one or more key and value pairs into a hash table. Because each key-value pair must be on a separate line, here-strings are often used as the input format. By default, the key must be separated from the value by an equals sign ( = ) character.

What happens when a hash table is full? ›

> When the hash table gets too full, we need to allocate a larger array and move the items over. This is absolutely required when the number of items in the hash table has reached the size of the array, but usually you want to do it when the table is half or three-quarters full.

How to convert JSON to HashTable in PowerShell? ›

-AsHashtable. Converts the JSON to a hash table object. This switch was introduced in PowerShell 6.0. Starting with PowerShell 7.3, the object is an OrderedHashtable and preserves the ordering of the keys from the JSON.

What is $_ in PowerShell? ›

PowerShell includes the $PSItem variable and its alias, $_ , as automatic variables in scriptblocks that process the current object, such as in the pipeline. This article uses $PSItem in the examples, but $PSItem can be replaced with $_ in every example.

How to print hashtable values in PowerShell? ›

Printing hashtable: To print /display a hashtable, just type the variable name in which hashtable is saved. The above command displays a table with two columns, one for keys and the other one for the associated values for the keys.

How to read data from Hashtable? ›

To do this we use the hashtable collection method in C#, Contains() that returns true if the key is present in the hashtable or false if the key is not present. If Contains() method returns true, then we simply access the value of that particular key. string keyval = (string)Citytable[key];

How to retrieve data from hash? ›

A hash table uses a hash function to convert the keys into hashes, and then stores the values in an array or a list at the corresponding index. This way, you can quickly access the value for any key by computing its hash and looking it up in the array or list.

How to get hash value of a file in PowerShell? ›

If you download the files, what you'd do from PowerShell is run “Get-FileHash” and specify the path to the file you want to be checked. You'll see it generated a hash value of 82F776A89483EB2192FC41637708A820938B9091D5964530A089092CB64AEBFB, and you can compare that to the value on the web page and verify it matches.

Top Articles
Latest Posts
Article information

Author: Carmelo Roob

Last Updated:

Views: 6433

Rating: 4.4 / 5 (65 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Carmelo Roob

Birthday: 1995-01-09

Address: Apt. 915 481 Sipes Cliff, New Gonzalobury, CO 80176

Phone: +6773780339780

Job: Sales Executive

Hobby: Gaming, Jogging, Rugby, Video gaming, Handball, Ice skating, Web surfing

Introduction: My name is Carmelo Roob, I am a modern, handsome, delightful, comfortable, attractive, vast, good person who loves writing and wants to share my knowledge and understanding with you.