How to check if an associative array is empty in powershell
24,212
Solution 1
That's not an associative array, it's a regular array, but the answer is the same. Use .Count
and compare to 0.
An associative array is called a [hashtable]
in PowerShell and its literal form uses @{}
(curly braces).
@{}.Count -eq 0 # hashtable (associative array)
@().Count -eq 0 # array
Solution 2
Arrays have Count
property, and you can check if this value is 0. So the condition you would check for is
$a.Count -eq 0
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Author by
Admin
Updated on July 09, 2022Comments
-
Admin almost 2 years
$a = @()
How do I check if
$a
above is empty (which it is). I would like to get$true
as answer.