Bird
Raised Fist0
PowerShellscripting~10 mins

Object 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 of objects with a single property 'Name'.

PowerShell
$array = @(@{Name='Alice'}, @{Name='Bob'}, @{Name='Charlie'})
$array | ForEach-Object { $_.[1] }
Drag options to blanks, or click blank then click option'
AObject
BValue
CName
DItem
Attempts:
3 left
💡 Hint
Common Mistakes
Using $_.Value or $_.Object which do not exist in the objects.
Trying to access the array directly without looping.
2fill in blank
medium

Complete the code to add a new object with Name 'David' to the existing array.

PowerShell
$array += [1]
Drag options to blanks, or click blank then click option'
A@{Name='David'}
B@{Value='David'}
C@{Object='David'}
D@{Item='David'}
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong property names like 'Value' or 'Object'.
Not using a hashtable syntax.
3fill in blank
hard

Fix the error in the code to filter objects where Name starts with 'A'.

PowerShell
$filtered = $array | Where-Object { $_.Name [1] '^A' }
Drag options to blanks, or click blank then click option'
A-contains
B-match
C-like
D-eq
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-eq' which checks exact equality.
Using '-like' without wildcards.
Using '-contains' which is for collections.
4fill in blank
hard

Fill both blanks to create an array of objects with Name and Age properties.

PowerShell
$people = @(@{Name='Eve'; [1]=30}, @{Name='Frank'; [2]=25})
Drag options to blanks, or click blank then click option'
AAge
BValue
CYears
DCount
Attempts:
3 left
💡 Hint
Common Mistakes
Using different property names like 'Value' or 'Years' inconsistently.
Using property names that do not match.
5fill in blank
hard

Fill all three blanks to create a filtered array of people older than 28 with their names in uppercase.

PowerShell
$result = $people | Where-Object { $_.[1] [2] 28 } | ForEach-Object { $_.[3].ToUpper() }
Drag options to blanks, or click blank then click option'
AAge
B>
CName
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong property names or operators.
Trying to uppercase the Age property.

Practice

(1/5)
1. What is an object array in PowerShell?
easy
A. A type of function that returns objects
B. A single object with many properties
C. A command to create new objects
D. A variable that holds multiple objects together in a list

Solution

  1. Step 1: Understand the definition of object arrays

    An object array is a collection that holds many objects in one variable, like a list.
  2. Step 2: Compare options with the definition

    Only A variable that holds multiple objects together in a list correctly describes an object array as a variable holding multiple objects.
  3. Final Answer:

    A variable that holds multiple objects together in a list -> Option D
  4. Quick Check:

    Object array = list of objects [OK]
Hint: Remember: arrays hold many items together [OK]
Common Mistakes:
  • Confusing object arrays with single objects
  • Thinking object arrays are commands
  • Mixing arrays with functions
2. Which of the following is the correct way to create an empty object array in PowerShell?
easy
A. $array = ()
B. $array = @()
C. $array = new-object array
D. $array = {}

Solution

  1. Step 1: Recall PowerShell syntax for empty arrays

    PowerShell uses @() to create an empty array.
  2. Step 2: Check each option

    $array = @() uses @(), which is correct. $array = () uses (), which assigns $null, not an array. $array = new-object array is incorrect syntax. $array = {} uses {}, which creates a hashtable, not an array.
  3. Final Answer:

    $array = @() -> Option B
  4. Quick Check:

    Empty array syntax = @() [OK]
Hint: Use @() to create empty arrays in PowerShell [OK]
Common Mistakes:
  • Using () which assigns null, not an array
  • Confusing arrays with hashtables {}
  • Trying to use new-object for arrays
3. What will be the output of this PowerShell code?
$array = @()
$array += 'apple'
$array += 'banana'
$array += 'cherry'
$array
medium
A. apple banana cherry
B. @('apple', 'banana', 'cherry')
C. apple banana cherry
D. Error: Cannot add to array

Solution

  1. Step 1: Understand how += adds items to arrays

    Using += adds each string as a new element to the array.
  2. Step 2: Output the array variable

    When output, PowerShell prints each element on a new line, so the output is three lines: apple, banana, cherry.
  3. Final Answer:

    apple banana cherry -> Option A
  4. Quick Check:

    Array elements print line by line [OK]
Hint: Arrays print each item on its own line [OK]
Common Mistakes:
  • Expecting all items on one line separated by spaces
  • Thinking += causes an error on arrays
  • Confusing array output with string output
4. Identify the error in this PowerShell script:
$array = @()
$array += 'one'
$array = $array + 'two'
$array = $array + 3
$array
medium
A. No error; script runs and outputs all items
B. Using + operator on arrays causes error
C. Cannot add integer to string array
D. Array must be declared with fixed size

Solution

  1. Step 1: Check adding different types to array

    PowerShell arrays can hold mixed types, so adding integer 3 is allowed.
  2. Step 2: Check use of + operator on arrays

    Using + concatenates arrays or adds items; it works fine here.
  3. Final Answer:

    No error; script runs and outputs all items -> Option A
  4. Quick Check:

    PowerShell arrays accept mixed types and + works [OK]
Hint: PowerShell arrays can mix types and use + to add items [OK]
Common Mistakes:
  • Thinking arrays only hold one data type
  • Believing + operator causes errors on arrays
  • Assuming arrays need fixed size
5. You have an array of objects representing people:
$people = @(
  [pscustomobject]@{Name='Alice'; Age=30},
  [pscustomobject]@{Name='Bob'; Age=25},
  [pscustomobject]@{Name='Carol'; Age=35}
)

How do you add a new person object with Name='Dave' and Age=40 to this array?
hard
A. $people = @($people, @{Name='Dave'; Age=40})
B. $people = $people + @{Name='Dave'; Age=40}
C. $people += [pscustomobject]@{Name='Dave'; Age=40}
D. $people.Add([pscustomobject]@{Name='Dave'; Age=40})

Solution

  1. Step 1: Create a new person object correctly

    Use [pscustomobject]@{Name='Dave'; Age=40} to create the new object.
  2. Step 2: Add the new object to the existing array

    Use += to add the new object to the $people array. $people += [pscustomobject]@{Name='Dave'; Age=40} does this correctly.
  3. Final Answer:

    $people += [pscustomobject]@{Name='Dave'; Age=40} -> Option C
  4. Quick Check:

    Add object to array using += [OK]
Hint: Use += with new [pscustomobject] to add to array [OK]
Common Mistakes:
  • Forgetting [pscustomobject] and adding plain hashtable
  • Using .Add() which does not work on arrays
  • Reassigning array incorrectly causing nested arrays
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