📖 Scenario: You work in IT support and need to find specific error messages in a log file quickly. Logs contain many lines, but you want to extract only those lines that mention a particular error code.
🎯 Goal: Build a PowerShell script that uses Select-String with a regular expression to find and display lines containing a specific error code from a list of log entries.
📋 What You'll Learn
Create a variable called logs containing an array of log entry strings.
Create a variable called errorCode with the exact error code string to search for.
Use Select-String with the -Pattern parameter set to a regex that matches the errorCode variable.
Print the matching log lines to the console.
💡 Why This Matters
🌍 Real World
IT professionals often need to scan large log files to find specific error messages quickly. Using Select-String with regex helps automate this task.
💼 Career
Skills in text searching and automation with PowerShell are valuable for system administrators and support engineers to troubleshoot and monitor systems efficiently.
Progress0 / 4 steps
1
Create the log entries array
Create a variable called logs that contains exactly these strings in an array: 'Info: System started', 'Warning: Low disk space', 'Error: Code1234 failure detected', 'Error: Code5678 failure detected', 'Info: User logged in'.
PowerShell
Hint
Use @( ... ) to create an array in PowerShell.
2
Set the error code to search
Create a variable called errorCode and set it to the string 'Code1234'.
PowerShell
Hint
Use = to assign the string 'Code1234' to errorCode.
3
Use Select-String with regex pattern
Use Select-String with the -Pattern parameter set to the regex pattern stored in errorCode to find matching lines in logs. Store the result in a variable called matches.
PowerShell
Hint
Use the pipeline | to send $logs to Select-String with -Pattern $errorCode.
4
Print the matching log lines
Print the matching lines stored in matches using Write-Output. Use .Line property to get the text of each match.
PowerShell
Hint
Use a foreach loop to go through $matches and print $match.Line.
Practice
(1/5)
1. What does the PowerShell cmdlet Select-String -Pattern "error" -Path "log.txt" do?
easy
A. Creates a new file named 'error' from 'log.txt'.
B. Deletes all lines containing 'error' from 'log.txt'.
C. Replaces the word 'error' with 'warning' in 'log.txt'.
D. Searches the file 'log.txt' for lines containing the word 'error'.
Solution
Step 1: Understand Select-String usage
Select-String searches text for a pattern, here 'error'.
Step 2: Analyze parameters
The -Pattern specifies the regex 'error', and -Path points to the file 'log.txt'.
Final Answer:
Searches the file 'log.txt' for lines containing the word 'error'. -> Option D
Quick Check:
Select-String with -Pattern finds matching lines [OK]
Hint: Select-String finds matching lines in files using regex [OK]
Common Mistakes:
Thinking Select-String edits files
Confusing -Pattern with replacement
Assuming it creates new files
2. Which of the following is the correct syntax to search for the word 'fail' in a file named 'output.log' using Select-String?
easy
A. Select-String -Pattern 'fail' -Path 'output.log'
B. Select-String -Find 'fail' -File 'output.log'
C. Select-String -Search 'fail' -FilePath 'output.log'
D. Select-String -Pattern fail -File output.log
Solution
Step 1: Recall correct parameter names
-Pattern specifies the regex pattern, and -Path specifies the file path.
Step 2: Match syntax to options
Select-String -Pattern 'fail' -Path 'output.log' uses -Pattern 'fail' and -Path 'output.log', which is correct syntax.
Final Answer:
Select-String -Pattern 'fail' -Path 'output.log' -> Option A
Quick Check:
Correct parameters are -Pattern and -Path [OK]
Hint: Use -Pattern for regex and -Path for file in Select-String [OK]
Common Mistakes:
Using -File instead of -Path
Using -Search or -Find which don't exist
Omitting quotes around pattern or path
3. Given a file 'data.txt' with the following lines:
apple
banana
apricot
blueberry
avocado
What will the command Select-String -Pattern '^a' -Path 'data.txt' output?
medium
A. Lines containing 'a' anywhere: apple, banana, apricot, avocado
B. Lines ending with 'a': banana
C. Lines starting with 'a': apple, apricot, avocado
D. No output because '^a' is invalid regex
Solution
Step 1: Understand the regex '^a'
The caret '^' means start of line, so '^a' matches lines starting with 'a'.
Step 2: Apply regex to file lines
Lines starting with 'a' are 'apple', 'apricot', and 'avocado'.
Final Answer:
Lines starting with 'a': apple, apricot, avocado -> Option C
Quick Check:
Regex '^a' matches line start with 'a' [OK]
Hint: ^ means line start in regex, so matches lines starting with 'a' [OK]
Common Mistakes:
Thinking '^a' matches anywhere in line
Confusing '^' with end of line
Assuming no matches due to regex error
4. You run Select-String -Pattern "[0-9]+" -Path "file.txt" but get no output, even though 'file.txt' contains numbers. What is the likely problem?
medium
A. The regex pattern is incorrect for matching numbers.
B. The file path is wrong or file does not exist.
C. Select-String does not support regex patterns.
D. The command needs -AllMatches to find numbers.
Solution
Step 1: Check regex pattern validity
The pattern "[0-9]+" correctly matches one or more digits.
Step 2: Consider file path and existence
If no output, likely the file path is incorrect or the file does not exist, so no lines are searched.
Final Answer:
The file path is wrong or file does not exist. -> Option B
Quick Check:
Valid regex but no output means file issue [OK]
Hint: Check file path and existence if no matches found [OK]
Common Mistakes:
Assuming regex is wrong when it's correct
Thinking Select-String can't use regex
Believing -AllMatches is required for any match
5. You want to find all lines in 'report.txt' that contain either 'error' or 'fail' (case-insensitive). Which command correctly does this?
hard
A. Select-String -Pattern '(?i)error|fail' -Path 'report.txt'
B. Select-String -Pattern 'error|fail' -Path 'report.txt' -CaseSensitive
C. Select-String -Pattern 'error|fail' -Path 'report.txt' -IgnoreCase
D. Select-String -Pattern '[errorfail]' -Path 'report.txt'
Solution
Step 1: Understand regex for 'error' or 'fail'
The pattern 'error|fail' matches either word. To ignore case, use '(?i)' inline regex flag.
Step 2: Analyze options for case-insensitivity
Select-String -Pattern '(?i)error|fail' -Path 'report.txt' uses (?i)error|fail which makes the pattern case-insensitive. Options B and C use invalid or non-existent parameters for case sensitivity.
Final Answer:
Select-String -Pattern '(?i)error|fail' -Path 'report.txt' -> Option A
Quick Check:
Use '(?i)' for case-insensitive regex in Select-String [OK]
Hint: Use '(?i)' in regex for case-insensitive matching [OK]
Common Mistakes:
Using -IgnoreCase which is not a Select-String parameter
Using character class '[errorfail]' which matches single chars
Using -CaseSensitive which enforces case sensitivity
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.