Table of Contents
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.
1 2 3 |
$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:
1 2 3 |
$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.
1 2 3 4 5 6 7 8 9 |
$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:
1 2 3 |
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.
1 2 3 4 5 6 7 8 9 |
$myArray = @("One", 2, "Three") if ($myArray.count -eq 0){ Write-Host "The array is empty." } else{ Write-Host "The array is not empty." } |
Output:
1 2 3 |
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.
1 2 3 4 5 6 7 8 9 |
$myArray = @() if ($myArray.length -eq 0){ Write-Host "The array is empty." } else{ Write-Host "The array is not empty." } |
Output:
1 2 3 |
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.
1 2 3 4 5 6 7 8 |
$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.
1 2 3 4 5 6 7 8 9 10 11 12 |
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.