The array operator, @(), can break up arrays that are used as arguments into operators, or values for parameters.
For example, the format operator, -f, may be used in place of sub-expressions or variable interpolation. @() may be used to define an array to hold the arguments for the operator. The following example shows two different ways of creating the same string:
$item = Get-Item C:Windowsexplorer.exe
# Sub-expressions and variable interpolation
"The file, $($item.Name), with path $item was last written on $($item.LastWriteTime)"
# The format operator
'The file, {0}, with path {1} was last written on {2}' -f @(
$item.Name
$item
$item.LastWriteTime
)
The same approach may be used for replace operations that use particularly long regular expressions. For example, this replace operation attempts to apply a standard format to a UK telephone number. The regular expression benefits from being on a new line:
$ukPhoneNumbers = '+442012345678', '0044(0)1234345678', '+44 (0) 20 81234567', '01234 456789'
$ukPhoneNumbers -replace @(
'^(?:(?:+|00)d{2})?[ -]*(?:(?0)?[ -]*)?([138]d{1,3}|20)[ -]*(d{3,4})[ -]*(d{3,4})$'
'+44 $1 $2 $3'
)
@() may also be used with arguments for commands, such as Select-Object:
Get-NetAdapter | Select-Object @(
'Name'
'Status'
'MacAddress'
LinkSpeed'
@{ Name = 'IPAddress'; Expression = { ($_ | Get-NetIPAddress).IPAddress }}
)
It is possible to add line breaks into the hashtable that describes the IPAddress property in the preceding example. Doing so may be beneficial if the Expression script grows to be complex.