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: $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: $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 = []
✗ Incorrect
In PowerShell, an empty array is created with @() syntax.
What is the index of the first element in a PowerShell array?
A1
B-1
C0
DIt depends on the array
✗ Incorrect
PowerShell arrays start counting at 0, so the first element is at index 0.
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
✗ Incorrect
Both += and + create a new array with the added item in PowerShell.
What will $array[10] return if $array has only 5 items?
ANull or empty
BAn error
CThe last item
DThe first item
✗ Incorrect
Accessing an index outside the array returns null or empty without error.
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
✗ Incorrect
PowerShell arrays support both .Length and .Count properties to get the number of items.
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.
Practice
(1/5)
1. What does an array in PowerShell do?
easy
A. Stores only one item at a time
B. Stores multiple items in one variable
C. Runs commands sequentially
D. Creates a new script file
Solution
Step 1: Understand the purpose of arrays
Arrays hold multiple values together in one variable, unlike single variables that hold one value.
Step 2: Compare options with array definition
Only Stores multiple items in one variable correctly describes storing multiple items in one variable, which matches the array concept.
Final Answer:
Stores multiple items in one variable -> Option B
Quick Check:
Arrays = store multiple items [OK]
Hint: Arrays hold many items together in one variable [OK]
Common Mistakes:
Thinking arrays hold only one item
Confusing arrays with scripts or commands
Believing arrays create files
2. Which of the following is the correct way to create an empty array in PowerShell?
easy
A. $arr = []
B. $arr = array()
C. $arr = {}
D. $arr = @()
Solution
Step 1: Recall PowerShell array syntax
PowerShell uses @() to create arrays, including empty ones.
Step 2: Check each option
$arr = @() uses @() which is correct. Options A, B, and C use invalid or different syntax for arrays.
Final Answer:
$arr = @() -> Option D
Quick Check:
Empty array = @() [OK]
Hint: Use @() to create arrays in PowerShell [OK]
Common Mistakes:
Using [] which is not valid for arrays in PowerShell
Using {} which is for hashtables or script blocks
Using array() which is not recognized in PowerShell
D. $arr += 'fish'; Write-Host $arr[0], $arr[1], $arr[2], $arr[3]
Solution
Step 1: Add 'fish' to the array correctly
Use '+=' operator to add 'fish' to $arr: $arr += 'fish'.
Step 2: Print each item on its own line
Use pipeline with ForEach-Object and Write-Output to print each item separately.
Step 3: Check each option
$arr += 'fish'; $arr | ForEach-Object { Write-Output $_ } correctly adds and prints items line by line. $arr + 'fish'; Write-Output $arr does not update $arr. $arr = $arr + 'fish'; Write-Host $arr uses Write-Host which prints all items on one line. $arr += 'fish'; Write-Host $arr[0], $arr[1], $arr[2], $arr[3] prints items but manually and is less flexible.