0
0
PowerShellscripting~5 mins

Arrays in PowerShell - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an array in PowerShell?
An array in PowerShell is a collection of items stored in a single variable. It can hold multiple values like numbers, strings, or objects, all together.
Click to reveal answer
beginner
How do you create an array with three numbers: 5, 10, and 15?
You create it by listing the values inside @() like this: <br>$numbers = @(5, 10, 15)
Click to reveal answer
beginner
How do you access the second item in an array named $colors?
Use the index number inside square brackets. Remember, counting starts at 0. So, the second item is $colors[1].
Click to reveal answer
intermediate
How can you add an item to an existing array in PowerShell?
Arrays are fixed size, but you can create a new array by adding items like this: <br>$newArray = $oldArray + 'newItem'
Click to reveal answer
beginner
What happens if you try to access an index that does not exist in an array?
PowerShell returns nothing (null) without an error. It just gives an empty result.
Click to reveal answer
How do you create an empty array in PowerShell?
A$array = ''
B$array = @()
C$array = {}
D$array = []
What is the index of the first element in a PowerShell array?
A1
B-1
C0
DIt depends on the array
Which of these adds an item 'apple' to an existing array $fruits?
A$fruits += 'apple'
BNeither A nor C
C$fruits = $fruits + 'apple'
DBoth A and C
What will $array[10] return if $array has only 5 items?
ANull or empty
BAn error
CThe last item
DThe first item
How do you find the number of items in an array $items?
ABoth B and C
B$items.Count
C$items.Length
D$items.Size
Explain how to create, access, and add items to an array in PowerShell.
Think about how you store a list of things and get or add items.
You got /3 concepts.
    What happens if you try to get an array item at an index that does not exist? How does PowerShell handle it?
    Imagine asking for a seat number that is not in the theater.
    You got /3 concepts.