PowerShell: Difference between array notation? PowerShell: Difference between array notation? powershell powershell

PowerShell: Difference between array notation?


$a = @() # declare an empty array.$a = @(mysingleitem) # declare an array with a single element

In other case is optional.


Is there a difference between these two array creation statements?

Though I am not 100% sure (it depends on PowerShell guts) the difference may be the following: "This", "Is", "a", "cat" creates an array. @("This", "Is", "a", "cat") creates the same array and then applies the operator @() to it (apparently redundant operation in this particular case).

Using, for example, this profiler we can see that the second expression is quite slower (14% or something), so that my guess may be correct. Ideally, PowerShell code interpretator could treat these two expressions in the same way but it probably does not.

See also the help topic (the end, about operators @() and ,)

help about_operators