Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is '@{}' meaning in PowerShell

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 = @{}?

like image 663
Mari yah Avatar asked Jul 10 '19 07:07

Mari yah


People also ask

What does $_ mean in PowerShell?

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.

What does @() mean in PowerShell?

Array subexpression operator @( )Returns the result of one or more statements as an array. The result is always an array of 0 or more objects. PowerShell Copy.

What does recurse mean in PowerShell?

The Recurse parameter gets items from the Path directory and its subdirectories. For example, -Path C:\Test\ -Recurse -Include *.txt. If a trailing asterisk ( * ) isn't included in the Path parameter, the command doesn't return any output and returns to the PowerShell prompt. For example, -Path C:\Test\ .

What is the meaning of {} in the help syntax in PowerShell?

For example, to use the Name parameter of New-Alias , you type the following: PowerShell Copy. -Name. Parameters can be mandatory or optional. In a syntax diagram, optional items are enclosed in brackets [ ] .


1 Answers

@{} 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.

like image 118
Ansgar Wiechers Avatar answered Oct 03 '22 15:10

Ansgar Wiechers