Challenge - 5 Problems
ForEach-Object Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Output of ForEach-Object with simple arithmetic
What is the output of this PowerShell command?
1..5 | ForEach-Object { $_ * 2 }PowerShell
1..5 | ForEach-Object { $_ * 2 }
Attempts:
2 left
💡 Hint
Remember $_ represents the current item in the pipeline.
✗ Incorrect
The command takes numbers 1 to 5 and multiplies each by 2, outputting 2 4 6 8 10.
💻 Command Output
intermediate2:00remaining
ForEach-Object with conditional output
What does this PowerShell command output?
1..4 | ForEach-Object { if ($_ % 2 -eq 0) { $_ } }PowerShell
1..4 | ForEach-Object { if ($_ % 2 -eq 0) { $_ } }
Attempts:
2 left
💡 Hint
Only even numbers are output because of the condition.
✗ Incorrect
The command outputs only numbers divisible by 2, which are 2 and 4.
📝 Syntax
advanced2:00remaining
Identify the syntax error in ForEach-Object usage
Which option contains a syntax error in using ForEach-Object?
Attempts:
2 left
💡 Hint
ForEach-Object requires a script block enclosed in braces {}.
✗ Incorrect
Option A misses the braces around the script block, causing a syntax error.
🔧 Debug
advanced2:00remaining
Why does this ForEach-Object command output only 4 and 5?
Given this command:
Why does it output only 4 and 5, but not 1, 2, 3?
1..5 | ForEach-Object { if ($_ -gt 3) { $_ } }Why does it output only 4 and 5, but not 1, 2, 3?
PowerShell
1..5 | ForEach-Object { if ($_ -gt 3) { $_ } }
Attempts:
2 left
💡 Hint
Check the condition inside the if statement.
✗ Incorrect
The if condition only outputs numbers greater than 3, so 4 and 5 are output.
🚀 Application
expert3:00remaining
Using ForEach-Object to modify and output a list
You have a list of filenames: 'file1.txt', 'file2.txt', 'file3.txt'.
Which command outputs the filenames without the '.txt' extension?
Which command outputs the filenames without the '.txt' extension?
Attempts:
2 left
💡 Hint
Use regex to remove only the '.txt' at the end of the string.
✗ Incorrect
Option D correctly uses -replace with regex '\.txt$' to remove the extension only at the end.