Search code examples
powershellhashtable

Why am I getting this Powershell hash literal was incomplete error?


I'm trying to set a hashtable (dictionary) in PowerShell. When I try this line:

$users = @{abertram = 'Adam Bertram' raquelcer = 'Raquel Cerillo' zheng21 = 'Justin Zheng'}

I get the following error:

At line:1 char:38
+ $users = @{abertram = 'Adam Bertram' raquelcer = 'Raquel Cerillo' zhe ...
+                                      ~~~~~~~~~
Unexpected token 'raquelcer' in expression or statement.
At line:1 char:37
+ $users = @{abertram = 'Adam Bertram' raquelcer = 'Raquel Cerillo' zhe ...
+                                     ~
The hash literal was incomplete.
At line:1 char:91
+ ... 'Adam Bertram' raquelcer = 'Raquel Cerillo' zheng21 = 'Justin Zheng'}
+                                                                         ~
Unexpected token '}' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

Why do I get this and how do I set a hash table in PowerShell correctly?


Solution

  • You have to separate Key/Value pairs on the same line with a semicolon ;

    $users = @{abertram = 'Adam Bertram'; raquelcer = 'Raquel Cerillo'; zheng21 = 'Justin Zheng'}
    

    It may be preferable to use new lines though:

    $users =
    @{
        abertram = 'Adam Bertram'
        raquelcer = 'Raquel Cerillo'
        zheng21 = 'Justin Zheng'
    }