Recently, one of my team members got a requirement from the client to create a variable with multiple values in PowerShell. I suggested a few useful methods. In this tutorial, I will explain how to create variables with multiple values in PowerShell with examples.
To create a variable with multiple values in PowerShell, use an array. An array stores multiple items in a single variable using the @() syntax, like $fruits = @('Apple', 'Banana', 'Cherry'). You can add more items with the += operator, such as $fruits += 'Date', 'Elderberry'. This method efficiently manages multiple values within one variable.
Create Variables with Multiple Values in PowerShell
In PowerShell, variables are used to store values that can be referenced and manipulated throughout your script. They can hold various data types, including strings, integers, arrays, and more complex objects.
When working with multiple values, arrays and hash tables are the best options in PowerShell. These structures allow you to store and manage collections of data efficiently.
Arrays
An array is a collection of items stored in a single variable. Each item in the array can be accessed using an index. This is the best option to create variables with multiple values in PowerShell.
To create an array in PowerShell, you can use the @() syntax:
# Creating an array
$myArray = @('Value1', 'Value2', 'Value3')You can also add multiple values to an existing array using the += operator:
# Adding values to an array
$myArray += 'Value4', 'Value5'Example
Let me show you an example.
Let’s say we want to create an array to store the names of different fruits:
# Creating an array of fruits
$fruits = @('Apple', 'Banana', 'Cherry')
# Adding more fruits to the array
$fruits += 'Date', 'Elderberry'
$fruitsYou can see the exact output in the screenshot below:

Check out Set a Variable to Null in PowerShell
Hash Tables
A hash table is a collection of key-value pairs. It is useful when you need to store related data together. So, this is another method for creating a variable with multiple values in PowerShell.
To create a hash table in PowerShell, you can use the @{} syntax:
# Creating a hash table
$myHashTable = @{
'Key1' = 'Value1'
'Key2' = 'Value2'
'Key3' = 'Value3'
}You can add more key-value pairs to an existing hash table like this:
# Adding key-value pairs to a hash table
$myHashTable['Key4'] = 'Value4'
$myHashTable['Key5'] = 'Value5'Now, let me show you an example.
Example
Suppose we want to create a hash table to store information about different cars:
# Creating a hash table of cars
$cars = @{
'Car1' = 'Toyota'
'Car2' = 'Honda'
'Car3' = 'Ford'
}
# Adding more cars to the hash table
$cars['Car4'] = 'Chevrolet'
$cars['Car5'] = 'Tesla'
$carsHere is the output you can see in the screenshot below:

Check out How to Increment A Variable in PowerShell?
Combine Arrays and Hash Tables
Sometimes, you may need to combine arrays and hash tables to handle more complex data structures. This way, you can create a variable with multiple values in PowerShell.
Example
Let’s create a hash table where each key holds an array of values:
# Creating a hash table with arrays as values
$inventory = @{
'Fruits' = @('Apple', 'Banana', 'Cherry')
'Vegetables' = @('Carrot', 'Broccoli', 'Spinach')
}
# Adding more items to the arrays
$inventory['Fruits'] += 'Date'
$inventory['Vegetables'] += 'Peas'Here is the output in the screenshot below:

Check out PowerShell Delete Variable
Dynamic Variables
In some scenarios, you might need to create variables dynamically. This can be achieved using the New-Variable cmdlet.
So, let me show you an example of how to create dynamic variables to store multiple values in PowerShell.
Example
Let’s dynamically create variables to store different types of beverages:
# Creating dynamic variables
New-Variable -Name 'Sodas' -Value @('Coke', 'Pepsi', 'Sprite')
New-Variable -Name 'Juices' -Value @('Orange', 'Apple', 'Grape')
# Accessing the dynamic variables
$Sodas
$JuicesConclusion
In this tutorial, I explained how to create variables with multiple values in PowerShell using different methods, such as arrays, hash tables, a combination of both, dynamic variables, etc.
You may also like:
- How to Check if a Variable Exists in PowerShell?
- Escape Special Characters in Variables in PowerShell
- Share Variables Between Functions in PowerShell
- Reset Variables in PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.