Getting HashTable length in powershell

I am new to powershell and trying to get the length of the HashTable (for use in the for loop), but I cannot force the length of the HashTable to output anything.

$user = @{} $user[0] = @{} $user[0]["name"] = "bswinnerton" $user[0]["car"] = "honda" $user[1] = @{} $user[1]["name"] = "jschmoe" $user[1]["car"] = "mazda" write-output $user.length #nothing outputs here for ($i = 0; $i -lt $user.length; $i++) { #write-output $user[0]["name"] } 
+6
source share
3 answers

@{} declares a HashTable, while @() declares an array

you can use

 $user.count 

to find the length of you HashTable .

If you do:

 $user | get-member 

You can see all the methods and properties of the object.

 $user.gettype() 

return the type of object you have.

+17
source

$user is a hash table, so you should use $user.count .

+3
source

This is not an array, but a hash table. Use .count :

 write-output $user.count 
0
source

All Articles