Bird
Raised Fist0
PowerShellscripting~10 mins

Arrays in PowerShell - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Arrays
Create Array
↓
Add Elements
↓
Access Elements by Index
↓
Modify Elements by Index
↓
Use Array in Commands or Loops
↓
End
This flow shows how to create an array, add or access elements by their position, modify them, and use the array in commands or loops.
Execution Sample
PowerShell
$arr = @(10, 20, 30)
$arr[1]
$arr[2] = 40
$arr
Create an array with three numbers, get the second element, change the third element, then show the whole array.
Execution Table
StepActionArray StateOutput
1Create array @(10, 20, 30)[10, 20, 30]
2Access element at index 1[10, 20, 30]20
3Modify element at index 2 to 40[10, 20, 40]
4Output whole array[10, 20, 40]10 20 40
💡 All steps completed; array created, accessed, modified, and displayed.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
$arrnull[10, 20, 30][10, 20, 30][10, 20, 40][10, 20, 40]
Key Moments - 3 Insights
Why does $arr[1] return 20 and not 10?
Arrays in PowerShell start at index 0, so $arr[0] is 10, $arr[1] is 20 as shown in execution_table step 2.
What happens if I try to access an index that does not exist?
PowerShell returns nothing; in this example, only indexes 0 to 2 exist as seen in variable_tracker and execution_table.
Does modifying $arr[2] change the original array?
Yes, as shown in step 3, $arr[2] changes from 30 to 40, updating the array in place.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output when accessing $arr[1]?
A20
B10
C30
D40
💡 Hint
Check execution_table row 2 under Output column.
At which step does the array change from [10, 20, 30] to [10, 20, 40]?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at execution_table row 3 and variable_tracker after Step 3.
If you add a new element $arr += 50 after step 4, what will be the final array?
A[10, 20, 40]
B[50]
C[10, 20, 40, 50]
D[10, 20, 30, 50]
💡 Hint
Adding with += appends the element, extending the array.
Concept Snapshot
Arrays in PowerShell:
- Created with @() syntax
- Index starts at 0
- Access elements with $arr[index]
- Modify elements by assigning $arr[index] = value
- Use arrays in loops or commands
- Adding elements with += appends to array
Full Transcript
This lesson shows how to work with arrays in PowerShell. First, you create an array using @() with elements inside. Then you can get an element by its position using square brackets, remembering that counting starts at zero. You can change an element by assigning a new value to that position. Finally, you can output the whole array to see all elements. Arrays are useful to store lists of items and use them in scripts or loops.

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

  1. Step 1: Understand the purpose of arrays

    Arrays hold multiple values together in one variable, unlike single variables that hold one value.
  2. 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.
  3. Final Answer:

    Stores multiple items in one variable -> Option B
  4. 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

  1. Step 1: Recall PowerShell array syntax

    PowerShell uses @() to create arrays, including empty ones.
  2. Step 2: Check each option

    $arr = @() uses @() which is correct. Options A, B, and C use invalid or different syntax for arrays.
  3. Final Answer:

    $arr = @() -> Option D
  4. 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
3. What is the output of this PowerShell code?
$arr = @('apple', 'banana', 'cherry')
Write-Output $arr[1]
medium
A. banana
B. apple
C. cherry
D. 1

Solution

  1. Step 1: Understand array indexing in PowerShell

    Arrays start at index 0, so $arr[0] = 'apple', $arr[1] = 'banana', $arr[2] = 'cherry'.
  2. Step 2: Identify the output of $arr[1]

    The code outputs the item at index 1, which is 'banana'.
  3. Final Answer:

    banana -> Option A
  4. Quick Check:

    Index 1 = banana [OK]
Hint: Arrays start at zero; index 1 is second item [OK]
Common Mistakes:
  • Thinking index starts at 1
  • Confusing item values with index numbers
  • Expecting all items printed instead of one
4. Identify the error in this PowerShell code:
$arr = @()
$arr + 'orange'
medium
A. += operator is missing to add item
B. Array cannot be empty
C. Use [] instead of @() for arrays
D. Variable name must start with capital letter

Solution

  1. Step 1: Check how to add items to arrays

    In PowerShell, to add an item to an array, use '+=' operator, not just '+'.
  2. Step 2: Analyze the code

    The code uses '$arr + 'orange'' which does not update $arr. It should be '$arr += 'orange'' to add the item.
  3. Final Answer:

    += operator is missing to add item -> Option A
  4. Quick Check:

    Add item with += [OK]
Hint: Use += to add items to arrays, not just + [OK]
Common Mistakes:
  • Using + without = to add items
  • Thinking arrays cannot be empty
  • Confusing array syntax with other languages
5. You have an array $arr = @('cat', 'dog', 'bird'). How do you add 'fish' to the end and then print all items one per line?
hard
A. $arr = $arr + 'fish'; Write-Host $arr
B. $arr + 'fish'; Write-Output $arr
C. $arr += 'fish'; $arr | ForEach-Object { Write-Output $_ }
D. $arr += 'fish'; Write-Host $arr[0], $arr[1], $arr[2], $arr[3]

Solution

  1. Step 1: Add 'fish' to the array correctly

    Use '+=' operator to add 'fish' to $arr: $arr += 'fish'.
  2. Step 2: Print each item on its own line

    Use pipeline with ForEach-Object and Write-Output to print each item separately.
  3. 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.
  4. Final Answer:

    $arr += 'fish'; $arr | ForEach-Object { Write-Output $_ } -> Option C
  5. Quick Check:

    Add with += and print with pipeline [OK]
Hint: Add with += and print each item using pipeline [OK]
Common Mistakes:
  • Using + without = to add items
  • Printing array without pipeline for separate lines
  • Using Write-Host which prints all on one line
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard