Introduction
Arrays help you store many items together in one place. This makes it easy to work with lists of things like names or numbers.
$arrayName = @('item1', 'item2', 'item3') # or $arrayName = @() $arrayName += 'newItem'
$fruits = @('apple', 'banana', 'cherry') Write-Output $fruits[1]
$emptyArray = @() $emptyArray += 'firstItem' Write-Output $emptyArray[0]
$numbers = @(10, 20, 30) Write-Output $numbers[-1]
$singleItemArray = @('onlyOne') Write-Output $singleItemArray[0]
# Create an array of colors $colors = @('red', 'green', 'blue') # Print all colors Write-Output "Colors before adding new one:" $colors # Add a new color $colors += 'yellow' # Print all colors again Write-Output "Colors after adding new one:" $colors
$arr = @('apple', 'banana', 'cherry')
Write-Output $arr[1]$arr = @()
$arr + 'orange'
$arr = @('cat', 'dog', 'bird'). How do you add 'fish' to the end and then print all items one per line?Pass the test and this pattern moves unknown