- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a Hash Table in PowerShell?
There are several ways to create a Hash Table in PowerShell. We will discuss here the standard method @{} for creating the Hash Table.
Using @{} Method
You can use @{} method to create a hash table. Key-Value pair is separated with the Semi-Colon (;). You can only add unique keys. Duplicate keys are not accepted.
$htable = @{EmpName="Charlie";City="New York";EmpID="001"}
Output
Name Value ---- ----- EmpID 001 City New York EmpName Charlie
Here, you will not get the output in an ordered fashion. To get the ordered output, you need to write [Ordered] ahead of the Hash table. For Example,
$htable = [Ordered]@{EmpName="Charlie";City="New York";EmpID="001"}
Output
PS C:\WINDOWS\system32> $htable Name Value ---- ----- EmpName Charlie City New York EmpID 001
To access only Hash table keys, you can use the below command.
$htable.Keys
Output
PS C:\WINDOWS\system32> $htable.Keys EmpName City EmpID
To access only Hash table values,
$htable.Values
Output
PS C:\WINDOWS\system32>$htable.Values Charlie New York 001
You can access the individual Key as well. To access the Employee Name (Key:EmpName), you need to use the below command.
$htable["EmpName"]
Output
PS C:\WINDOWS\system32> $htable["EmpName"] Charlie
You can also access the same value with the Dot (.) method as shown below.
$htable.EmpName
Similarly, you can access different values by using respective keys.
To get the multiple values, use multiple keys.
$htable["EmpName","EmpID"]
Output
PS C:\WINDOWS\system32> $htable["EmpName","EmpID"] Charlie 001