Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell: Adding element to hashtable failed

PS C:\Users\Hind> $b=@{}
PS C:\Users\Hind> $b+={k="a";v="b"}
A hash table can only be added to another hash table.
At line:1 char:1
+ $b+={k="a";v="b"}
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : AddHashTableToNonHashTable

Why did it fail? How can I add one element to a hashtable successfully?

like image 906
Hind Forsum Avatar asked Feb 08 '23 19:02

Hind Forsum


1 Answers

Correction, this fails because you are missing the @ character in front of @{k="a";b="b"}

PS C:\Users\Hind> $b=@{}
PS C:\Users\Hind> $b+=@{k="a";v="b"}

@{} is declaring a new hash table. {} is a script block. They are not the same.

like image 109
Daniel Lesser Avatar answered Feb 10 '23 09:02

Daniel Lesser