How to Clear an Array in PowerShell?

Do you need to clear a PowerShell array to remove all elements and start fresh? This PowerShell tutorial will guide you through different methods to clear an array in PowerShell using various methods and examples.

To clear an array in PowerShell, you can simply reinitialize it to an empty array using $array = @() which will remove all elements from the array. Alternatively, if you want to maintain the original array type and clear its contents, you can use $array.Clear() if it’s an ArrayList or a similar collection type that supports the .Clear() method.

Clear an Array in PowerShell

An array is a data structure that serves as a collection of multiple items. These items can be of the same type or different types. Arrays are useful when you need to work with a list of objects or values in a structured way.

To declare and initialize an array in PowerShell, you can use the following syntax:

$array = @(1, 2, 3, 4, 5)

This creates an array with five integers. You can access individual items using their index, starting at 0 for the first element.

Now, let us check a few methods of how to clear an array in PowerShell.

When it comes to clearing a PowerShell array, you have a few methods. Let’s explore each of them.

Method 1: Reinitialize the Array

The simplest way to clear an array in PowerShell is to reinitialize it. You can do this by assigning an empty array to the variable:

$array = @()

This effectively clears the array by making it an empty array with no elements.

Here is a complete PowerShell script:

# Declare and initialize an array
$colors = @('Red', 'Green', 'Blue')

# Clear the array by reinitializing
$colors = @()

# Check if the array is cleared
$colors.Count # This will output 0

Once you execute the PowerShell script using VS code, you can see the output in the screenshot below.

Clear an Array in PowerShell

Method 2: Using Clear() Method on ArrayList

If you’re working with an ArrayList, you can use the Clear() method to remove all elements from the ArrayList. Here’s how you can do it:

$arrayList = New-Object System.Collections.ArrayList
$arrayList.AddRange(@(1, 2, 3, 4, 5))
$arrayList.Clear()

After calling $arrayList.Clear(), the ArrayList will be empty.

Here is a complete script on how to clear a PowerShell array.

# Create an ArrayList and add elements
$numbers = New-Object System.Collections.ArrayList
$numbers.AddRange(@(10, 20, 30))

# Clear the ArrayList
$numbers.Clear()

# Verify that the ArrayList is cleared
$numbers.Count # Outputs 0

Method 3: Using Clear-Variable Cmdlet

PowerShell provides a cmdlet called Clear-Variable that clears the value of a variable. When used with an array, it will remove all elements from the array:

$array = @(1, 2, 3, 4, 5)
Clear-Variable -Name array

After executing Clear-Variable, the $array variable will be empty.

Here is the complete PowerShell script.

# Create an array with some elements
$names = @('Alice', 'Bob', 'Charlie')

# Use Clear-Variable to clear the array
Clear-Variable -Name names

# Check that the array is cleared
$names.Count # Outputs 0

Method 4: Using RemoveAll Method on Generic List

If you’re using a generic list, you can use the RemoveAll method to clear all the items:

$list = New-Object System.Collections.Generic.List[Object]
$list.AddRange(@(1, 2, 3, 4, 5))
$list.RemoveAll({$true})

This will remove all items from $list because the script block {$true} always returns true, which means all items match the condition and hence are removed.

Here is the complete PowerShell script, you can use to clear a PowerShell array.

# Initialize a List of integers
$intList = New-Object System.Collections.Generic.List[int]
$intList.AddRange(@(1, 2, 3, 4, 5))

# Clear the List using RemoveAll
$intList.RemoveAll({$true})

# Verify that the List is empty
$intList.Count # Outputs 0

Conclusion

In this PowerShell tutorial, I have explained how to clear an array in PowerShell using various methods with real examples.

You may also like: