How to Create an Empty Array in PowerShell?

Do you need to create an empty array in PowerShell? In this PowerShell tutorial, I will explain how to create an empty array in PowerShell and work with PowerShell empty arrays.

To create an empty array in PowerShell, assign an array literal to a variable using $myArray = @(). This command initializes $myArray as an empty array, allowing you to subsequently add elements to it with $myArray += 'newElement'.

Why Use an Empty Array in PowerShell?

An empty array in PowerShell is essentially a starting point. It’s a container ready to be filled with elements. Creating an empty array is useful when you know you will need to store a collection of items but don’t yet have the items to store at the time of array creation. You can then add elements to the array as they become available.

Create an Empty Array in PowerShell

The simplest way to create an empty array in PowerShell is by using the array subexpression operator @(). Here’s how you do it:

$myArray = @()

This command initializes $myArray as an empty array. At this point, $myArray contains no elements.

Adding Elements to an Empty Array

Once you have an empty array, you might want to add elements to it. Although PowerShell arrays are fixed in size once created, you can still simulate adding elements by creating a new array that includes the elements of the existing array plus the new element. Here’s an example:

$myArray += "new element"

This line takes the current $myArray, adds a new element to it, and then assigns the result back to $myArray. Keep in mind that this process is not the most efficient for adding many elements because it creates a new array with each addition.

You can look at the screenshot below, I have created an empty array in PowerShell using VS code.

powershell create empty array

Check out PowerShell Append to Array

Alternative Methods to Create an Empty Array

Another way to create an empty array in PowerShell is to cast a variable as an array type. Here’s an example using the array cast [array]:

$myArray = [array]@()

This method makes it explicitly clear that $myArray is intended to be an array data type.

Create an empty array with properties in PowerShell

Creating an empty array with properties in PowerShell involves defining an object with the desired properties and then storing these objects in an array. This is often done using custom objects. Here’s an example:

First, define a custom object with the properties you want. For instance:

$customObject = New-Object PSObject -Property @{
    Property1 = $null
    Property2 = $null
}

In this example, Property1 and Property2 are the names of the properties, and they are initially set to $null.

Next, create an empty array to store such objects:

$myArray = @()

Now, you can add the custom object to the array:

$myArray += $customObject

This array, $myArray, now contains one item, which is a custom object with Property1 and Property2. You can add more objects with the same properties to the array using the same method. Each object can have its properties set to different values as needed.

Create an empty string array in PowerShell

In PowerShell, an empty array of strings is an array that is initialized without any string elements. To create an empty array of strings, you use the @() syntax, which denotes an array. Since you’re specifying an array of strings, it will be understood that the array is intended to hold string elements, but initially, it contains none.

Here’s an example of how you can create an empty array of strings in PowerShell:

$stringArray = @()

This command initializes $stringArray with an empty array. You can then add strings to this array using various methods, such as the += operator to append an element:

$stringArray += "First string"
$stringArray += "Second string"

After these operations, $stringArray will contain two elements: “First string” and “Second string”.

PowerShell: create an empty array with headers

In PowerShell, an “array with headers” typically refers to a collection of objects with named properties, rather than a traditional array. To create such a collection, you can use a PSCustomObject or a hashtable, and then store these objects in an array.

Here’s an example of how to create an empty array that will hold objects with headers (named properties):

# Define an empty array to hold the custom objects
$objectArray = @()

# Define a new object with named properties (headers)
$object = New-Object PSObject -Property @{
    Header1 = $null
    Header2 = $null
    Header3 = $null
}

# Add the object to the array
$objectArray += $object

In this example, $objectArray is an array that is intended to hold objects with three properties: Header1Header2, and Header3. Initially, the array contains one object with all properties set to $null.

Create an empty array of size in PowerShell

In PowerShell, you can create an array with a predefined size, but it’s important to note that arrays in PowerShell are fixed in size once they are created. To create an array with a specific size, you typically initialize the array with a certain number of elements. However, since PowerShell arrays are not like traditional statically-sized arrays found in languages like C#, the approach is a bit different.

For example, if you want to create an array that can hold 10 elements, you might use the following code:

# Create an array with 10 elements, all initialized to $null
$arrayOfSize = New-Object object[] 10

In this example, $arrayOfSize is an array that has 10 elements, each initialized to $null. You can then assign values to each element of the array by accessing them using their index.

Here’s how you can assign a value to the first element (index 0) of the array:

$arrayOfSize[0] = "First element"

If you want to create an array that is initialized with a default value other than $null, you can use a loop to populate the array. For instance, to create an array of 10 empty strings:

# Create an array of 10 empty strings
$arrayOfSize = 1..10 | ForEach-Object { "" }

This uses a ForEach-Object loop to set each of the 10 elements to an empty string ("").

Conclusion

Creating an empty array with @() provides a flexible starting point for collecting and manipulating a set of items in your script. Remember that while PowerShell arrays are fixed in size, you can simulate adding, modifying, and removing elements by creating new arrays or filtering existing ones.

In this PowerShell tutorial, I have explained how to create an empty array in PowerShell using various methods.

You may also like: