Powershell

What does @() mean in PowerShell Script?

Arrays are one of the most important and widely used concepts in any programming language. In PowerShell, there are multiple ways to create an array, and among them, the most simple and easy way to create an array is the array subexpression operator i.e. @(). By default, the @() operator creates an empty array, however, it can take some statements in it, and in such cases, the @() operator will create an array of objects depending upon the statements specified within it.

This post will cover the below-listed aspects of the array sub-expression operator:

So, let’s begin!

What is @() in PowerShell Script?

In PowerShell, the array subexpression operator “@()” is used to create an array. To do that, the array sub-expression operator takes the statements within the parentheses and produces the array of objects depending upon the statements specified in it. All in all, we can say that the @() operator is used to get the array of zero, one, or more objects.

Syntax

The basic syntax of the array sub-expression operator is shown in the below-given snippet:

@(statement)

What Does @() Operator Return in PowerShell?

Any number of objects (i.e. zero, one, or more) produced/returned by the array sub-expression will be presented as an array (i.e. each object will be placed inside the array).

Examples

In this section we will consider a couple of examples to get a basic understanding of the @() operator in PowerShell.

How to create an array of zero object

$name = @()

The above snippet will create an array of zero objects. We can use the count operator to count the number of objects. To do that, follow the below-given syntax:

$name.count

The below snippet will show the respective output:

The output shows that the @() operator creates an array of zero objects.

How to create an array of one object

$name = @("linuxhint")

$name.count

In the above snippet, we utilized the @() operator to create an array and afterward we utilized the count operator to count the number of objects in the array. We get the following output upon successful execution of the code:

The output shows that the array subexpression operator @() produces an array of one object.

How to create an array of multiple object

$name = @("C++","Java","JavaScript")

$name.count

In this example, firstly, we created an array of multiple objects. Afterward, we utilized the count operator to count the number of objects. The above code block will produce the following output:

The above snippet shows that the array “name” has three objects in it.

Conclusion

In PowerShell, the array subexpression operator “@()” takes some statements within the parentheses and produces the array of objects depending upon the statements specified in it. The array subexpression operator @() wraps the output within an array if the output is not already an array i.e. it is used to get the array of zero, one, or more objects. This write-up explained various aspects of the array subexpression @() operator with the help of some suitable examples.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.