What is '@{}' meaning in PowerShell

powershell, powershell-4.0

Solution

`@{}` in PowerShell defines a hashtable, a data structure for mapping unique keys to values (in other languages this data structure is called "dictionary" or "associative array").

`@{}` on its own defines an empty hashtable, that can then be filled with values, e.g. like this:

$h = @{}
$h['a'] = 'foo'
$h['b'] = 'bar'

Hashtables can also be defined with their content already present:

$h = @{
    'a' = 'foo'
    'b' = 'bar'
}

Note, however, that when you see similar notation in PowerShell output, e.g. like this:

abc: 23
def: @{"a"="foo";"b"="bar"}

that is usually not a hashtable, but the string representation of a custom object.

Problem

I have line of scripts for review here, I noticed variable declaration with a value: ``` function readConfig { Param([string]$fileName) $config = @{} Get-Content $fileName | Where-Object { $_ -like '*=*' } | ForEach-Object { $key, $value = $_ -split '\s*=\s*', 2 $config[$key] = $value } return $config } ``` I wonder what `@{}` means in `$config = @{}`?

Original source