0
0
PowerShellscripting~20 mins

Select-String for searching in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Select-String Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Select-String command?
Given a file named log.txt with the following content:
Info: Start process
Error: File not found
Warning: Low memory
Error: Access denied

What will be the output of this command?
Select-String -Path log.txt -Pattern 'Error'
PowerShell
Select-String -Path log.txt -Pattern 'Error'
A
Info: Start process
Warning: Low memory
B
Error: File not found
Error: Access denied
CError: File not found
D
Warning: Low memory
Error: Access denied
Attempts:
2 left
💡 Hint
Select-String finds all lines containing the pattern.
💻 Command Output
intermediate
2:00remaining
What does this Select-String command output?
Consider this command:
Get-Content data.txt | Select-String -Pattern '^Start'

If data.txt contains:
End of file
Start of process
Starting point
Stop here

What lines will be output?
PowerShell
Get-Content data.txt | Select-String -Pattern '^Start'
AStart of process
B
End of file
Stop here
C
Start of process
Starting point
DStarting point
Attempts:
2 left
💡 Hint
The caret ^ matches the start of a line.
🔧 Debug
advanced
2:00remaining
Why does this Select-String command fail?
You run this command:
Select-String -Path 'C:\Logs\app.log' -Pattern Error -CaseSensitive

But it returns an error:
Select-String : A parameter cannot be found that matches parameter name 'CaseSensitive'.

What is the cause?
PowerShell
Select-String -Path 'C:\Logs\app.log' -Pattern Error -CaseSensitive
AThe file path is incorrect, causing the error.
BThe pattern 'Error' must be in quotes.
CThe '-CaseSensitive' parameter does not exist; use '-CaseSensitive' as a switch is invalid.
DSelect-String requires '-IgnoreCase' instead of '-CaseSensitive'.
Attempts:
2 left
💡 Hint
Check the official parameters of Select-String.
🚀 Application
advanced
2:00remaining
How to find lines NOT containing a pattern using Select-String?
You want to find all lines in server.log that do NOT contain the word 'Warning'. Which command achieves this?
ASelect-String -Path server.log -Pattern '.*' | Where-Object { $_.Line -notmatch 'Warning' }
BSelect-String -Path server.log -Pattern 'Warning' -NotMatch
CSelect-String -Path server.log -Pattern 'Warning' -NotMatch $true
DSelect-String -Path server.log -Pattern 'Warning' -NotMatch $false
Attempts:
2 left
💡 Hint
Select-String does not have a '-NotMatch' parameter.
🧠 Conceptual
expert
2:00remaining
What is the count of matches from this Select-String command?
Given a file data.txt with 5 lines:
apple
banana
Apple pie
APPLE
pineapple

What is the output of:
(Select-String -Path data.txt -Pattern 'apple' -AllMatches).Matches.Count
PowerShell
(Select-String -Path data.txt -Pattern 'apple' -AllMatches).Matches.Count
A1
B3
C5
D4
Attempts:
2 left
💡 Hint
By default, Select-String is case-insensitive.