Friday, October 28, 2022

[FIXED] How to check if an associative array is empty in powershell

Issue

$a = @() 

How do I check if $a above is empty (which it is). I would like to get $true as answer.


Solution

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


Answered By - briantist
Answer Checked By - Mary Flores (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.