Use the @ Symbol in PowerShell

Rohan Timalsina Jan 30, 2023 Mar 16, 2022
  1. Use the @ Symbol to Create an Array in PowerShell
  2. Use the @ Symbol to Create a Hash Table in PowerShell
  3. Use the @ Symbol for Here-Strings in PowerShell
  4. Use the @ Symbol to Perform Splatting With Arrays in PowerShell
  5. Use the @ Symbol to Perform Splatting With Hash Tables in PowerShell
Use the @ Symbol in PowerShell

You might have seen the different symbols used in PowerShell, such as @, ?, |, $, %, and =. All these symbols have their usage in the command.

This tutorial will show you the various usage of the @ symbol in PowerShell.

Use the @ Symbol to Create an Array in PowerShell

The array subexpression operator @() creates and initializes an array in PowerShell.

$array = @('Apple', 'Banana', 'Mango')
$array

Output:

Apple
Banana
Mango

You can use the GetType method to get the data type of a variable.

$array.GetType()

Output:

IsPublic  IsSerial  Name                                     BaseType
--------  --------  ----                                     --------
True      True      Object[]                                 System.Array

You can use this operator to create an array of zero or one object. If no values are provided in the brackets, it will create an empty array.

$data = @()

Use the @ Symbol to Create a Hash Table in PowerShell

A hash table is a compact data structure that stores one or more paired keys and values. It is also known as a dictionary or associative array.

The syntax to create a hash table in PowerShell begins with the @ symbol. The keys and values are enclosed in the {} brackets.

The following example creates a hash table with three keys and values.

$hash = @{Name = "Sam"; Age = 21; Address = "Nepal"}
$hash

Output:

Name                           Value
----                           -----
Name                           Sam
Age                            21
Address                        Nepal

You can use the GetType() method to check the data type.

$hash.GetType()

Output:

IsPublic  IsSerial  Name                                     BaseType
--------  --------  ----                                     --------
True      True      Hashtable                                System.Object

An empty hash table will be created if no keys and values are provided in the brackets.

$hash = @{}

Use the @ Symbol for Here-Strings in PowerShell

Another important use of the @ symbol is to define here-strings in PowerShell. Quotation marks are interpreted literally in a here-string.

A here-string can be a single-quoted or double-quoted string having @ on each end. It can have multiple lines of text.

Here-string with double quotes:

@"
Press "Enter" to continue...
Press "X" to quit.
"@

Output:

Press "Enter" to continue...
Press "X" to quit.

A here-string contains all the texts between @" "@.

Here-string with single quotes:

@'
My name is $name.
'@

Output:

My name is $name.

In single-quote here-strings, variables are interpreted literally and printed in the output but not in double-quotes here-strings.

Use the @ Symbol to Perform Splatting With Arrays in PowerShell

You can use the @ symbol for splatting arrays in the command. The @ symbol can use an array to splat values for position parameters that do not need the names of parameters.

We will compare the two examples of the Copy-Item cmdlet to copy test.txt to test2.txt in the same directory.

The following example uses the basic format where parameter names are omitted.

The -WhatIf parameter shows what would happen if the cmdlet runs. The cmdlet is not run.

Copy-Item "test.txt" "test2.txt" -WhatIf

Output:

What if: Performing the operation "Copy File" on target "Item: C:\Users\rhntm\test.txt Destination: C:\Users\rhntm\test2.txt".

Now, let’s perform the same operation using array splatting. This example creates an array of the parameter values in the $ArrayArguments variable.

Another command uses the $ArrayArguments variable in splatting by replacing the $ symbol with @ in the command.

$ArrayArguments = "test.txt", "test2.txt"
Copy-Item @ArrayArguments -WhatIf

Output:

What if: Performing the operation "Copy File" on target "Item: C:\Users\rhntm\test.txt Destination: C:\Users\rhntm\test2.txt".

Use the @ Symbol to Perform Splatting With Hash Tables in PowerShell

Similarly, you can use the @ symbol for hash table splatting.

The first command creates a hash table to store the parameter-name and parameter-value pairs in the $HashArguments variable. The second uses the $HashArguments variable in splatting by replacing the $ symbol with @ in the command.

$HashArguments = @{
    Path = "test.txt"
    Destination = "test2.txt"
    WhatIf = $true
}
Copy-Item @HashArguments

You can provide a value $true or $false for the -WhatIf parameter.

Output:

What if: Performing the operation "Copy File" on target "Item: C:\Users\rhntm\test.txt Destination: C:\Users\rhntm\test2.txt".

As you can see, it performs the same operation with the hash table splatting.

Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website