154

I've seen the @ symbol used in PowerShell to initialise arrays.

What exactly does the @ symbol denote and where can I read more about it?

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
GrahamMc
  • 3,034
  • 2
  • 24
  • 29

6 Answers6

139

In PowerShell V2, @ is also the Splat operator.

PS> # First use it to create a hashtable of parameters:
PS> $params = @{path = "c:\temp"; Recurse= $true}
PS> # Then use it to SPLAT the parameters - which is to say to expand a hash table 
PS> # into a set of command line parameters.
PS> dir @params
PS> # That was the equivalent of:
PS> dir -Path c:\temp -Recurse:$true
user2368632
  • 990
  • 1
  • 12
  • 33
Jeffrey Snover - MSFT
  • 10,173
  • 4
  • 21
  • 13
  • 3
    This is what I was looking for: this operator is used in the splat context in Azure Resource Group deployment scripts. – Alex Marshall Aug 03 '16 at 13:37
105

PowerShell will actually treat any comma-separated list as an array:

"server1","server2"

So the @ is optional in those cases. However, for associative arrays, the @ is required:

@{"Key"="Value";"Key2"="Value2"}

Officially, @ is the "array operator." You can read more about it in the documentation that installed along with PowerShell, or in a book like "Windows PowerShell: TFM," which I co-authored.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Don Jones
  • 9,367
  • 8
  • 39
  • 49
100

While the above responses provide most of the answer it is useful--even this late to the question--to provide the full answer, to wit:

Array sub-expression (see about_arrays)

Forces the value to be an array, even if a singleton or a null, e.g. $a = @(ps | where name -like 'foo')

Hash initializer (see about_hash_tables)

Initializes a hash table with key-value pairs, e.g. $HashArguments = @{ Path = "test.txt"; Destination = "test2.txt"; WhatIf = $true }

Splatting (see about_splatting)

Let's you invoke a cmdlet with parameters from an array or a hash-table rather than the more customary individually enumerated parameters, e.g. using the hash table just above, Copy-Item @HashArguments

Here strings (see about_quoting_rules)

Let's you create strings with easily embedded quotes, typically used for multi-line strings, e.g.:

$data = @"
line one
line two
something "quoted" here
"@

Because this type of question (what does 'x' notation mean in PowerShell?) is so common here on StackOverflow as well as in many reader comments, I put together a lexicon of PowerShell punctuation, just published on Simple-Talk.com. Read all about @ as well as % and # and $_ and ? and more at The Complete Guide to PowerShell Punctuation. Attached to the article is this wallchart that gives you everything on a single sheet: enter image description here

Michael Sorens
  • 35,361
  • 26
  • 116
  • 172
32

You can also wrap the output of a cmdlet (or pipeline) in @() to ensure that what you get back is an array rather than a single item.

For instance, dir usually returns a list, but depending on the options, it might return a single object. If you are planning on iterating through the results with a foreach-object, you need to make sure you get a list back. Here's a contrived example:

$results = @( dir c:\autoexec.bat)

One more thing... an empty array (like to initialize a variable) is denoted @().

TylerH
  • 20,799
  • 66
  • 75
  • 101
Mike Shepard
  • 17,466
  • 6
  • 51
  • 69
13

The Splatting Operator

To create an array, we create a variable and assign the array. Arrays are noted by the "@" symbol. Let's take the discussion above and use an array to connect to multiple remote computers:

$strComputers = @("Server1", "Server2", "Server3")<enter>

They are used for arrays and hashes.

PowerShell Tutorial 7: Accumulate, Recall, and Modify Data

Array Literals In PowerShell

li ki
  • 342
  • 3
  • 11
Cadoo
  • 807
  • 5
  • 13
0

I hope this helps to understand it a bit better. You can store "values" within a key and return that value to do something. In this case I have just provided @{a="";b="";c="";} and if not in the options i.e "keys" (a, b or c) then don't return a value

$array = @{
a = "test1";
b = "test2";
c = "test3"
}

foreach($elem in $array.GetEnumerator()){
    if ($elem.key -eq "a"){
        $key = $elem.key
        $value = $elem.value
    }
    elseif ($elem.key -eq "b"){
        $key = $elem.key
        $value = $elem.value
    }
    elseif ($elem.key -eq "c"){
        $key = $elem.key
        $value = $elem.value
    }
    else{
        Write-Host "No other value"
    }

    Write-Host "Key: " $key "Value: " $value 
}