Bird
Raised Fist0
PowerShellscripting~20 mins

Where-Object for filtering in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Where-Object Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of filtering numbers greater than 5
What is the output of this PowerShell command?
1..10 | Where-Object { $_ -gt 5 }
PowerShell
1..10 | Where-Object { $_ -gt 5 }
A
6
7
8
9
10
B
1
2
3
4
5
6
C
5
6
7
8
9
10
D
1
2
3
4
5
Attempts:
2 left
💡 Hint
Remember, Where-Object filters items where the condition is true.
💻 Command Output
intermediate
2:00remaining
Filtering strings containing 'a'
What is the output of this PowerShell command?
@('apple','banana','cherry','date') | Where-Object { $_ -like '*a*' }
PowerShell
@('apple','banana','cherry','date') | Where-Object { $_ -like '*a*' }
A
banana
cherry
date
B
apple
banana
date
C
apple
cherry
D
cherry
date
Attempts:
2 left
💡 Hint
The -like operator checks if the string contains the pattern.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in filtering
Which option contains a syntax error in using Where-Object to filter even numbers from 1 to 5?
PowerShell
1..5 | Where-Object { $_ % 2 -eq 0 }
A1..5 | Where-Object { $_ % 2 = 0 }
B} 0 qe- 2 % _$ { tcejbO-erehW | 5..1
C1..5 | Where-Object { $_ % 2 -eq 0 }
D..5 | Where-Object { $_ % 2 -eq 0 }
Attempts:
2 left
💡 Hint
Check the operator used for comparison.
🔧 Debug
advanced
2:00remaining
Why does this filter return no results?
Given this command:
@('cat','dog','bird') | Where-Object { $_ -ceq 'Cat' }

Why does it return no output?
PowerShell
@('cat','dog','bird') | Where-Object { $_ -ceq 'Cat' }
ABecause -ceq operator is invalid here
BBecause the array is empty
CBecause PowerShell -ceq string comparison is case-sensitive
DBecause the script block is missing a return statement
Attempts:
2 left
💡 Hint
Check the case sensitivity of the comparison operator used.
🚀 Application
expert
3:00remaining
Filter processes using Where-Object with multiple conditions
Which command filters processes with CPU usage greater than 100 and process name starting with 's' (case-insensitive)?
AGet-Process | Where-Object { $_.CPU -gt 100 -and $_.Name -like 's*' }
BGet-Process | Where-Object { $_.CPU -gt 100 -or $_.Name -ilike 's*' }
CGet-Process | Where-Object { $_.CPU > 100 -and $_.Name -like 's*' }
DGet-Process | Where-Object { $_.CPU -gt 100 -and $_.Name -ilike 's*' }
Attempts:
2 left
💡 Hint
Use the correct operator for case-insensitive matching and logical AND.

Practice

(1/5)
1. What does the Where-Object cmdlet do in PowerShell?
easy
A. Filters objects in a collection based on a condition
B. Sorts objects alphabetically
C. Creates new objects from existing ones
D. Deletes objects from a collection

Solution

  1. Step 1: Understand the purpose of Where-Object

    Where-Object is used to filter items in a collection by applying a condition to each item.
  2. Step 2: Compare options with the purpose

    Only Filters objects in a collection based on a condition correctly describes filtering based on a condition. Sorting, creating, or deleting are different operations.
  3. Final Answer:

    Filters objects in a collection based on a condition -> Option A
  4. Quick Check:

    Where-Object filters collections = A [OK]
Hint: Where-Object selects items matching a condition [OK]
Common Mistakes:
  • Confusing filtering with sorting
  • Thinking it creates or deletes objects
  • Ignoring the condition part
2. Which of the following is the correct syntax to filter processes with CPU usage greater than 100 using Where-Object?
easy
A. Get-Process | Where-Object { CPU > 100 }
B. Get-Process | Where-Object { $_.CPU -gt 100 }
C. Get-Process | Where-Object CPU -gt 100
D. Get-Process | Where-Object CPU > 100

Solution

  1. Step 1: Identify correct use of script block and $_ variable

    Where-Object requires a script block with curly braces and uses $_ to refer to the current object.
  2. Step 2: Check each option's syntax

    Get-Process | Where-Object { $_.CPU -gt 100 } correctly uses { $_.CPU -gt 100 }. Versions without the script block braces {} are incorrect. The version with { CPU > 100 } misses $_ before CPU.
  3. Final Answer:

    Get-Process | Where-Object { $_.CPU -gt 100 } -> Option B
  4. Quick Check:

    { $_.Property -gt value } syntax = D [OK]
Hint: Always use { $_.Property -operator value } inside Where-Object [OK]
Common Mistakes:
  • Omitting curly braces {}
  • Not using $_ to reference current object
  • Writing condition outside script block
3. What will be the output of this command?
1..5 | Where-Object { $_ % 2 -eq 0 }
medium
A. [1, 3, 5]
B. [1, 2, 3, 4, 5]
C. [2, 4]
D. Error: Invalid operator

Solution

  1. Step 1: Understand the input and condition

    The input is numbers 1 to 5. The condition filters numbers where remainder when divided by 2 equals 0 (even numbers).
  2. Step 2: Apply the condition to each number

    Numbers 2 and 4 satisfy the condition (2 % 2 = 0, 4 % 2 = 0). Others do not.
  3. Final Answer:

    [2, 4] -> Option C
  4. Quick Check:

    Filter even numbers = [2, 4] [OK]
Hint: Use modulo (%) to filter even numbers [OK]
Common Mistakes:
  • Confusing even and odd numbers
  • Expecting all numbers as output
  • Thinking syntax causes error
4. Identify the error in this command:
Get-Service | Where-Object $_.Status -eq 'Running'
medium
A. Missing curly braces around the condition
B. Incorrect property name 'Status'
C. Wrong operator used for comparison
D. No error, command is correct

Solution

  1. Step 1: Check Where-Object syntax

    Where-Object requires the condition inside curly braces {} to form a script block.
  2. Step 2: Analyze the given command

    The command misses curly braces around $_.Status -eq 'Running', causing syntax error.
  3. Final Answer:

    Missing curly braces around the condition -> Option A
  4. Quick Check:

    Where-Object needs { condition } [OK]
Hint: Always wrap condition in { } for Where-Object [OK]
Common Mistakes:
  • Omitting curly braces
  • Assuming property names are wrong without checking
  • Confusing operators
5. You have a list of files with sizes in bytes. Which command filters files larger than 1MB (1048576 bytes) and selects only their names?
Get-ChildItem | Where-Object { $_.Length -gt 1048576 } | ???
What should replace ????
hard
A. Sort-Object Length
B. Where-Object Name
C. Format-Table Length
D. Select-Object Name

Solution

  1. Step 1: Understand the filtering part

    Where-Object filters files with Length greater than 1048576 bytes (1MB).
  2. Step 2: Identify how to select only file names

    Select-Object Name extracts only the Name property from the filtered files.
  3. Final Answer:

    Select-Object Name -> Option D
  4. Quick Check:

    Select-Object Name for properties = C [OK]
Hint: Use Select-Object to pick specific properties [OK]
Common Mistakes:
  • Using Where-Object instead of Select-Object to pick properties
  • Sorting instead of selecting
  • Formatting output without selecting needed data
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