Bird
Raised Fist0
PowerShellscripting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the PowerShell cmdlet Select-String primarily do?
easy
A. Creates new text files with specified content
B. Deletes files matching a pattern
C. Searches for text patterns in files or command output
D. Lists all files in a directory

Solution

  1. Step 1: Understand the purpose of Select-String

    Select-String is used to find text patterns inside files or output streams.
  2. Step 2: Compare options with Select-String's function

    Only Searches for text patterns in files or command output describes searching text patterns, which matches Select-String's role.
  3. Final Answer:

    Searches for text patterns in files or command output -> Option C
  4. Quick Check:

    Select-String = Search text [OK]
Hint: Select-String = find text inside files or output [OK]
Common Mistakes:
  • Confusing Select-String with file deletion commands
  • Thinking it creates or lists files
  • Assuming it modifies file content
2. Which of the following is the correct syntax to search for the word 'error' in a file named 'log.txt' using Select-String?
easy
A. Select-String -Pattern error -Path log.txt
B. Select-String log.txt error
C. Select-String -File log.txt -Search error
D. Select-String -Find error -File log.txt

Solution

  1. Step 1: Recall Select-String parameter names

    The correct parameters are -Pattern for the search text and -Path for the file.
  2. Step 2: Match syntax with options

    Select-String -Pattern error -Path log.txt uses -Pattern and -Path correctly; others use invalid parameters.
  3. Final Answer:

    Select-String -Pattern error -Path log.txt -> Option A
  4. Quick Check:

    Correct parameters = Select-String -Pattern error -Path log.txt [OK]
Hint: Use -Pattern for text and -Path for files [OK]
Common Mistakes:
  • Using incorrect parameter names like -File or -Find
  • Omitting parameter names and causing errors
  • Mixing order of parameters incorrectly
3. What will be the output of this command if the file 'notes.txt' contains the line 'PowerShell is fun'?
Select-String -Pattern 'fun' -Path notes.txt
medium
A. Error: Pattern not found
B. PowerShell is fun
C. No output
D. notes.txt:1:PowerShell is fun

Solution

  1. Step 1: Understand Select-String output format

    Select-String outputs the file name, line number, and matching line by default.
  2. Step 2: Match output with file content

    The line contains 'fun', so output shows 'notes.txt:1:PowerShell is fun'.
  3. Final Answer:

    notes.txt:1:PowerShell is fun -> Option D
  4. Quick Check:

    Output includes filename and line = notes.txt:1:PowerShell is fun [OK]
Hint: Select-String shows filename:line#:matched text [OK]
Common Mistakes:
  • Expecting only matched text without filename
  • Assuming no output if pattern is found
  • Confusing output with error messages
4. You run this command but get an error:
Select-String -Pattern 'test' log.txt
What is the most likely cause?
medium
A. File 'log.txt' does not exist
B. Pattern 'test' is invalid syntax
C. Missing -Path parameter before the file name
D. Select-String cannot search text files

Solution

  1. Step 1: Check command syntax

    The command uses correct syntax: -Pattern 'test' followed by the file path positionally.
  2. Step 2: Identify most likely error cause

    If an error occurs, it's most likely because the file 'log.txt' does not exist.
  3. Final Answer:

    File 'log.txt' does not exist -> Option A
  4. Quick Check:

    Syntax OK, file must exist [OK]
Hint: Verify file exists before searching [OK]
Common Mistakes:
  • Assuming syntax error when file is missing
  • Blaming pattern syntax
  • Thinking Select-String can't handle text files
5. You want to find all lines containing 'error' or 'fail' in multiple log files inside the 'C:\Logs' folder. Which command correctly does this?
hard
A. Select-String -Pattern 'error,fail' -Path 'C:\Logs\*.log'
B. Select-String -Pattern 'error|fail' -Path 'C:\Logs\*.log'
C. Select-String -Pattern 'error fail' -Path 'C:\Logs\*.log'
D. Select-String -Pattern 'error','fail' -File 'C:\Logs\*.log'

Solution

  1. Step 1: Understand pattern syntax for multiple words

    Using 'error|fail' means search for 'error' OR 'fail' as a regex pattern.
  2. Step 2: Check parameter correctness

    -Path is correct for specifying files; -File is invalid parameter.
  3. Step 3: Evaluate options

    Select-String -Pattern 'error|fail' -Path 'C:\Logs\*.log' uses regex OR pattern and correct parameters; others misuse commas or parameters.
  4. Final Answer:

    Select-String -Pattern 'error|fail' -Path 'C:\Logs\*.log' -> Option B
  5. Quick Check:

    Regex OR pattern with -Path = Select-String -Pattern 'error|fail' -Path 'C:\Logs\*.log' [OK]
Hint: Use regex 'error|fail' with -Pattern for OR search [OK]
Common Mistakes:
  • Using commas in -Pattern instead of regex OR
  • Using invalid -File parameter
  • Writing patterns as space-separated words
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