Powershell

What Does the “@” Symbol do in PowerShell

The “@” symbol in PowerShell is used for many operations. For instance, adding round braces with this symbol makes an array sub-expression operator “@()”, which can be used to create an array. Moreover, if this symbol is combined with the rectangle braces “@[ ]”, it can create a hash table. Further, it can be utilized to construct a multiline string.

This write-up will observe details about the “@” symbol in PowerShell.

What Does the “@” Symbol Do in PowerShell?

The “@” symbol in PowerShell can be utilized to:

Method 1: Create an Array Using the “@” Symbol in PowerShell

The “@” symbol combines with the pair of round braces “()” to make an array sub-expression. The array sub-expression operator “@()” helps in creating an array.

Example

For instance, check out this example to create an array:

$array = @("Cat", "Dog", "Horse")

 
In accordance with the above code:

    • First, initiate a variable “$array” for storing an array.
    • To start an array, first, specify the “@” symbol and add objects inside it separated by a comma within small braces:


Let’s display the objects inside the array by invoking the array assigned variable:

$array

 

Method 2: Create a Hash Table in PowerShell Using the “@” Symbol

The “@” symbol when combined with the curly braces “{}” defines a hash table, whose symbol is “@{}”. The hash table keys and values are added inside it.

Example

In this illustration, the creation of the hash table will be demonstrated:

$hash = @{
 key1 = "val_1"
 key2 = "val_2"
 key3 = "val_3"
}

 
According to the above code:

    • First, initiate a variable “$hash” and assign a hash table to it.
    • To create a hash table, first, specify the “@” symbol, then add multiple keys and assign them values inside curly braces:


Let’s output the created hash table by calling the variable it is assigned:

Write-output $hash

 
To display the hash table in the PowerShell console:

First, write the “Write-output” cmdlet and specify the hash table assigned variable:


Let’s display the single hash table value in the PowerShell console:

write-output $hash["key1"]

 
To display the single hash value in the PowerShell console:

First, write the “write-output” cmdlet, specify the hash table assigned variable, and add the key value name to be displayed inside the square braces:


That was all about the “@” symbol in PowerShell.

Conclusion

The “@” symbol in PowerShell when combined with a pair of round braces makes an array sub-expression operator, which eventually creates an array. However, when this symbol is combined with the rectangle braces, it can create a hash table. This blog has covered all the scenarios and demonstrations of the “@” symbol.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.