📖 Scenario: You have a text file with several lines of text. You want to find all lines that contain a specific word.
🎯 Goal: Build a PowerShell script that uses Select-String to search for a word in a text file and display the matching lines.
📋 What You'll Learn
Create a text file variable with exact content
Create a search word variable
Use Select-String to find lines containing the search word
Display the matching lines
💡 Why This Matters
🌍 Real World
Searching text files for specific words or patterns is common in system administration and automation tasks.
💼 Career
Knowing how to use Select-String helps you quickly find important information in logs or configuration files.
Progress0 / 4 steps
1
Create a text file with content
Create a variable called filePath and set it to the string "sample.txt". Then create a text file named sample.txt with these exact lines: PowerShell is great I love scripting Searching text is easy Automation saves time
PowerShell
Hint
Use Out-File to create the text file with the lines.
2
Set the search word
Create a variable called searchWord and set it to the string "scripting".
PowerShell
Hint
Just assign the string "scripting" to the variable searchWord.
3
Use Select-String to find matching lines
Use Select-String with the variables filePath and searchWord to find all lines in the file that contain the search word. Store the result in a variable called matches.
PowerShell
Hint
Use Select-String -Path $filePath -Pattern $searchWord and assign it to matches.
4
Display the matching lines
Print only the matching lines from the matches variable using Write-Output.
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 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
Step 1: Understand the purpose of Select-String
Select-String is used to find text patterns inside files or output streams.
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.
Final Answer:
Searches for text patterns in files or command output -> Option C
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
Step 1: Recall Select-String parameter names
The correct parameters are -Pattern for the search text and -Path for the file.
Step 2: Match syntax with options
Select-String -Pattern error -Path log.txt uses -Pattern and -Path correctly; others use invalid parameters.
Final Answer:
Select-String -Pattern error -Path log.txt -> Option A