TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

PowerShell Hast Table

PowerShell Hast Table with What is PowerShell, History of PowerShell, Features of PowerShell, PowerShell vs CMD, PowerShell Scripting, PowerShell Versions, PowerShell Commands, PowerShell Looping, PowerShell Conditions, PowerShell Array, PowerShell Brackets etc.

<< Back to POWERSHELL

PowerShell Hast table

The PowerShell Hashtable is a data structure that stores one or more key/value pairs. It is also known as the dictionary or an associative array.

In the PowerShell, there exists a Hashtable (System.Collections.Hashtable) object for each hash table.

We can use the properties and methods of the Hashtable object in the PowerShell. The key and the values in the Hash table are also the objects of .NET type.

After the PowerShell version 3.0 introduced, we can use the [ordered] attribute to create an ordered dictionary (System.Collections.Specialized.OrderedDictionary) in PowerShell.

The main difference between the ordered dictionaries and the Hashtable is that the keys in dictionaries always appear in the order in which we list them. But the order of the keys in the Hashtable is not determined.

Syntax

The following statement is the syntax to create the Hashtable:

$variable_name = @{ <key1> = <value1> ; < key2> = <value2> ; ..... ; < keyN> = <valueN>;}

The following statement is the syntax to create an ordered dictionary:

$variable_name = [ordered] @{ < key1> = <value1> ; < key2> = <value2> ; ..... ; < keyN> = <valueN>;}

Create a Hash Table

The following are the steps to create the hash table in the PowerShell:

  1. Create a hast table with the @ symbol at the beginning.
  2. Enclose the hash table in the braces.
  3. Enter one or more key/value pairs as the content of the hash table.
  4. To separate each value form it's key, we must use an equal sign (=).
  5. To separate the key/value pairs, we must use a semi colon (;) or the line break.
  6. Those keys which contain the spaces enclosed them in a quotation marks. And the values must be valid expression of PowerShell.
  7. To manage the Hast table, assign it to the variables.
  8. When you assign the ordered hast table to the variable, you can place the ordered attribute before @ sign.

If you want to create an empty hash table, type the following command in the PowerShell:

$variablename = @{}

We can also add the keys and values to the hash table when we create it.

The following example describe how to create the hash table with three keys and their values.

$student = @{ name = "Abhay" ; Course = "BCA" ; Age= 19 }

Display a Hash table

  • To display the hash table, type the name of the variable which stores it. By default, it displays the table with two columns. One column is for the keys and another for their values.
    The following command displays the result of hash table:
$Student

Output:

Name             Value
----             -----
Course           BCA
name             Sumit
Age              20 
  • To display all the keys or all the values of the hash table, use the dot (.) notation. The following example displays all the keys of the above example:
$Student.keys

Output:

Course
name
Age

The following example displays all the values of the above example:

$Student.values

Output:

BCA
Sumit
20 
  • Hash tables have a "count" property, which indicates the total number of key/value pairs in the hash table. The following command will display the total number of key-value pairs in the above example:
$Student.count

Output:

3





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf