Check if Array is Empty in PowerShell

1. Introduction

In PowerShell, determining if an array is empty is a fundamental task often encountered in scripting. An "empty" array here refers to one that contains no elements.

For example, given an array $myArray, our goal is to ascertain whether it’s empty and act accordingly.

In this article, we will see different ways to check if Array is empty in PowerShell.

2. What is an Empty Array?

An empty array is an array with no elements. The length of an empty array is zero.

The following example creates an empty array in PowerShell.

$myArray = @()

3. Using the Count Property

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

The command to display the number of elements in an array is:

$myArray.count

If an array is empty, the number of elements is zero. If it is not empty, the number of elements is greater than 0.

We can apply this condition in if statement to check if an array is empty.

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

The if block only executes if the $array.count equals zero.

Output:

The array is empty.

This method is very fast and efficient for any array size.

Now, let’s run the script with the non-empty array.

$myArray = @("One", 2, "Three")
if ($myArray.count -eq 0){
Write-Host "The array is empty."
}
else{
Write-Host "The array is not empty."
}

Output:

The array is not empty.

4. Using Length Property

Another way to check if array is empty is to use Length property similar to Count one.

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

Output:

The array is empty.

Here, $myArray.length returns number of elements in the array. Since we have initialized array with 0 elements, it will return 0.

5. Using LINQ

For more advanced scenarios, especially when dealing with large data sets or when we are already using LINQ for other operations, we can use LINQ to check for an empty array.

$myArray = @()
if ([System.Linq.Enumerable]::Count($myArray) -eq 0) {
    "Array is empty"
} else {
    "Array is not empty"
}

LINQ’s Count() method is used to count the elements in the array.

6. Using Custom Function

We can create custom function in case we need to check empty array frequently.

function isEmptyArray($array) {
    return $array.Count -eq 0
}

$myArray = @()
if (isEmptyArray $myArray) {
    "Array is empty"
} else {
    "Array is not empty"
}

This method is only recommended for scripts where the emptiness check is frequent and to improve script maintainability.

7. Conclusion

In this article, we have discussed multiple ways to check if Array is empty in PowerShell.

For most scenarios, using the Count or Length properties provides a straightforward and efficient solution.

LINQ offers an advanced alternative, especially beneficial in complex scripts or large data sets. Creating a custom function can further streamline your code, making it more readable and maintainable.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.