Jul 23, 2014 - One of the most usefull features in PowerShell are arrays, i already talked about them in a previous post. Today i'll write about their sister:.
I wanted to do
[string]::Join(', ', $HashTable.Keys);
but that returns System.Collections.Hashtable+KeyCollection...
instead of the enumerated values.Do I need to make a foreach loop for this?
ДМИТРИЙ МАЛИКОВ15.1k9 gold badges58 silver badges116 bronze badges
ruirui7,2885 gold badges36 silver badges59 bronze badges
1 Answer
Shay LevyShay Levy90.4k17 gold badges144 silver badges160 bronze badges
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Not the answer you're looking for? Browse other questions tagged arraysstringpowershellhashtable or ask your own question.
- September 22, 2015
- Posted by: Surender Kumar
- Category: Windows PowerShell
Hash Tables in Windows 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.
Ordered dictionaries differ from hash tables in that the keys always appear in the order in which you list them. The order of keys in a hash table is not determined.
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.
![Add array to hashtable powershell Add array to hashtable powershell](/uploads/1/2/3/7/123721244/644871009.png)
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.
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.
To get the members of a hash table, pipe the hash table to Get-Member cmdlet.
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.
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.
Add or Remove the Keys and Values
To add keys and values to a hash table, use the following command format.
$hash['<key>”] = “<value>”
For example, to add a “DomainName” key with a value of “TechTutsOnline.com” to the hash table, use the following command:
![Convert Convert](/uploads/1/2/3/7/123721244/425867433.png)
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 $hash variable.
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=TechTutsOnline.com key/value pair from the hash table in the value of the $hash variable, use the following command:
In the same way you can use all of the properties and methods of Hashtable objects in Windows PowerShell.
Next