Arrays are a fundamental feature of PowerShell. Arrays make it possible to ingest, manipulate and output true data structures (and not just raw strings). This capability makes PowerShell different and more useful than other scripting languages.
There are three methods to create and define an array. Based on these three methods, below are the three syntaxes of a PowerShell array:
In the first syntax, an array is created by enclosing the items in the @() parentheses.
$ArrayList = @('item 1', 'item 2', 'item 3')
The second syntax creates an array with items in a comma-separated list.
$ArrayList = 'item 1', 'item 2', 'item 3'
The third syntax creates an array with a specific data type.
[ArrayType[]]$ArrayList = 'item 1', 'item 2', 'item 3'
An array, once created, stays the same size forever, and so in order to make it larger, you need to copy it to a new array, and delete the original.
PowerShell can add two arrays together using the “+” operator, which hides the complexity of what the system is actually doing. For instance, if you make two test arrays like this:
$first = @('Zero', 'One') $second = @('Two', 'Three')
You can then add them together using just:
PS> $first + $second
This will make a new array with all four values, and output the results. Note, however, that it will not give this new array a new name. To do that, we use the += operator.
Instead of using the “+” operator in the above example, we could have used the “+=” operator, which will give us a whole new array:
$first += 'Two, Three'
The command looks simple, but it is hiding what PowerShell is actually doing here. It is copying all of the values from $first, and making a new array that includes the extra values. It then deletes the original array.
This is somewhat problematic because it is computationally expensive. With small arrays, you might not notice, but with large amounts of data this method can quickly lock up your system.
To access an item in an array we can use an index.
$ArrayList = @('item 1', 'item 2', 'item 3')
$ArrayList [1]
item 2
We can use the Unique switched parameter from Select-Object or Sort-Object cmdlet to remove duplicate items and get only distinct items from an array.
$Array = @('one', 'two','three','three','four','five')
$Result = $Array | Select-Object -Unique
$Result #List only unique items
The Unique parameter with the Select-Object cmdlet performs case-sensitive string comparison, so we need to use it with the Sort-Object cmdlet to perform a case-insensitive check.
$Array = @('one', 'two','three','Three','four')
#Remove duplicates by case-sensitive check.
$Array | Select-Object -Unique
#Output – one, two, three, Three, four
#Remove duplicates by case-insensitive check.
$Array | Sort-Object -Unique
#Output - four, one, three, two
In real-time application requirements, we will work with a custom PowerShell object array (complex object) instead of a simple string or integer array. The Unique switched parameter can also be used to find distinct objects by a property in a custom object array.
# Initialize the array $Array = @() $Array += [PSCustomObject]@{ Name = "User1"; } $Array += [PSCustomObject]@{ Name = "User2"; } $Array += [PSCustomObject]@{ Name = "User2"; } $Array += [PSCustomObject]@{ Name = "User3"; } $ResultArray = $Array | Select-Object -Unique -Property Name $ResultArray # Result array with unique Name values Name ---- User1 User2 User3
As already explained, the Unique parameter with Select-Object cmdlet finds unique values with case-sensitive string comparison. If you work with a string property and want to perform a case-insensitive string comparison to find distinct objects, then we need to use the Unique parameter with Sort-Object cmdlet to perform a case-insensitive check.
# Initialize the array $Array = @() $Array += [PSCustomObject]@{ Name = "User1"; } $Array += [PSCustomObject]@{ Name = "user2"; } $Array += [PSCustomObject]@{ Name = "User2"; } $Array += [PSCustomObject]@{ Name = "User3"; } # Find unique objects by case-sensitive comparison. $Array | Select-Object -Unique -Property Name Name ---- User1 user2 User2 User3 # Find unique objects by case-insensitive comparison. $Array | Sort-Object -Unique -Property Name Name ---- User1 User2 User3