How to Check if an Array is Empty in PowerShell?

Do you want to check if an array is empty in PowerShell? In this blog post, we’ll explore various methods to determine if an array is empty in PowerShell.

To check if an array is empty in PowerShell, you can test if the count of the array is zero by using $array.Count -eq 0. If this condition returns $true, the array is empty; otherwise, it contains one or more elements. You can also use $array -eq $null to check if the array is uninitialized or has been explicitly set to $null.

Check if an Array is Empty in PowerShell

An array is a data structure that holds multiple values in a single variable. Each item in an array is called an element, and each element is accessible through its index.

Here’s how you can create an array in PowerShell:

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

This array contains five elements, each with a numerical value.

Now, let us see how to check if the above array is empty in PowerShell.

1. Using the Count Property

One of the simplest ways to check if an array is empty in PowerShell is by using the Count property. This property returns the number of elements in the array.

Here’s how you can use it:

$myArray = @()
if ($myArray.Count -eq 0) {
    Write-Host "The array is empty."
} else {
    Write-Host "The array is not empty."
}

If the Count property is equal to 0; the PowerShell array is empty.

You can see the output in the screenshot below after I executed the script using Visual Studio code.

Check if an Array is Empty in PowerShell

2. Using the Length Property

Similar to the Count property, the Length property can also be used to determine if an array is empty in PowerShell. The Length property also returns the number of elements in the array.

Here’s an example using the Length property in PowerShell array:

$myArray = @()
if ($myArray.Length -eq 0) {
    Write-Host "The array is empty."
} else {
    Write-Host "The array is not empty."
}

Check out the screenshot below for the output.

PowerShell Check if an Array is Empty

3. Using Conditional Statements

PowerShell treats empty arrays as $false when evaluated in a boolean context. This means you can use a simple if statement to check if an array is empty.

Here’s an example:

$myArray = @()
if ($myArray) {
    Write-Host "The array is not empty."
} else {
    Write-Host "The array is empty."
}

In this case, if $myArray is empty, the else block will execute, printing “The array is empty.”

4. Using the Any() Method

For PowerShell versions 4.0 and above, you can use the Any() method to check if an array is not empty. The Any() method is a part of LINQ (Language Integrated Query) and it returns True if the array contains at least one element.

Here’s how to use the Any() method:

$myArray = @()
if ($myArray.Any()) {
    Write-Host "The array is not empty."
} else {
    Write-Host "The array is empty."
}

However, keep in mind that if you’re working with an array that could potentially contain $null elements, you might want to filter those out first before calling Any().

Understanding Null and Empty Arrays in PowerShell

It’s important to distinguish between a null array and an empty array. A null array is a variable that has not been assigned any value, not even an empty array. An empty array, on the other hand, is an array that has been initialized but contains no elements.

Here’s an example to illustrate the difference:

$nullArray = $null
$emptyArray = @()

Write-Host "Null Array Count: $($nullArray.Count)"
Write-Host "Empty Array Count: $($emptyArray.Count)"

In this case, trying to access the Count property on a null array will result in an error because $null does not have a Count property. The empty array will correctly report a count of 0.

Conclusion

Checking if an array is empty is a common task in PowerShell scripting. By using the Count or Length properties or by taking advantage of how PowerShell evaluates arrays in boolean contexts, you can easily determine if an array contains elements.

In this PowerShell tutorial, I have explained the below methods to check if an array is empty in PowerShell:

  1. Using the Count Property
  2. Using the Length Property
  3. Using Conditional Statements
  4. Using the Any() Method

You may also like: