Bird
Raised Fist0
PowerShellscripting~10 mins

Arrays in PowerShell - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create an array with three numbers.

PowerShell
$numbers = @([1])
Drag options to blanks, or click blank then click option'
A1 2 3
B1, 2, 3
C1; 2; 3
D1. 2. 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using semicolons instead of commas to separate values.
Using dots instead of commas.
2fill in blank
medium

Complete the code to access the second element of the array.

PowerShell
$secondElement = $numbers[[1]]
Drag options to blanks, or click blank then click option'
A1
B3
C2
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 2 or 3 as the index for the second element.
Forgetting that indexing starts at zero.
3fill in blank
hard

Fix the error in the code to add an element to the array.

PowerShell
$numbers [1] 4
Drag options to blanks, or click blank then click option'
A*=
B-=
C+
D+=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' which does not modify the original array.
Using '-=' or '*=' which are incorrect here.
4fill in blank
hard

Fill both blanks to create an array of numbers from 4 to 12.

PowerShell
$evens = @((2[1]10)[2]2)
Drag options to blanks, or click blank then click option'
A..
B,
C-
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using commas instead of the range operator.
Using '-' instead of '+'.
5fill in blank
hard

Fill all three blanks to create a filtered array of numbers greater than 3.

PowerShell
$filtered = $numbers | Where-Object { $_ [1] [2] } | Sort-Object -Descending | Select-Object -First [3]
Drag options to blanks, or click blank then click option'
A-gt
B3
C2
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators like '-lt'.
Selecting wrong number of elements.

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