Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an associative array is empty in powershell

$a = @() 

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

like image 985
Spencer E Avatar asked May 05 '16 19:05

Spencer E


People also ask

What is a hashtable in PowerShell?

A PowerShell hash table is data structure of key/value pairs. A key/value pair is essentially a set of two elements that are related in some manner. In its simplest form, a hash table is just a way to store one or more sets of item names and item values.


1 Answers

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
like image 171
briantist Avatar answered Oct 15 '22 20:10

briantist