- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 add multiple values in the Hashtable using PowerShell?
Example
For example, we have a below-created hashtable.
PS C:\> $servicehash = @{Name='Spooler';State='Stopped';StartType='Automatic'} PS C:\> $servicehash
Output
Name Value ---- ----- Name Spooler StartType Automatic State Stopped
We need to add multiple values to the Name Key. If we directly append the value to the key it will treat it as a string and we will not get the desired output. See the example below.
Example
PS C:\> $servicehash.Name += "Winrm" PS C:\> $servicehash
Output
Name Value ---- ----- Name SpoolerWinrm StartType Automatic State Stopped
So to add the multiple values, we first need to declare a key as an array but by doing so it will clear the previous value so we have to add the previous value as well to the array.
Example
PS C:\> $servicehash.Name = @() PS C:\> $servicehash.Name += "Spooler" PS C:\> $servicehash.Name += "Winrm" PS C:\> $servicehash
Output
Name Value ---- ----- Name {Spooler, Winrm} StartType Automatic State Stopped
PS C:\> $servicehash.Name Spooler Winrm
- Related Articles
- How to convert the hashtable to the JSON format using PowerShell?
- How to convert JSON object to Hashtable format using PowerShell?
- How to use Hashtable splatting in PowerShell?
- How to add/remove values in the array in PowerShell?
- How to convert Dictionary to Hashtable in PowerShell?
- How to add and remove values to the Hash Table in PowerShell?
- How to stop multiple services using PowerShell?
- How to convert command output to the Hashtable format in PowerShell?
- How to generate multiple reports in HTML using PowerShell?
- How to start multiple windows services using PowerShell?
- How to copy multiple files in PowerShell using Copy-Item?
- How to add a new element in the XML using PowerShell?
- How to add the tag of Azure VM using PowerShell?
- How to add the entry in the windows host file using PowerShell?
- How to add an attribute to the XML file using Powershell?

Advertisements