0
0
PowerShellscripting~5 mins

Regex with Select-String in PowerShell - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the <code>Select-String</code> cmdlet do in PowerShell?
It searches through text or files for patterns that match a regular expression (regex) and returns the matching lines.
Click to reveal answer
beginner
How do you use Select-String to find the word 'apple' in a file named fruits.txt?
Use the command: <br>Select-String -Path fruits.txt -Pattern 'apple'<br>This searches for 'apple' in the file and shows matching lines.
Click to reveal answer
beginner
What is a regular expression (regex) in simple terms?
A regex is like a search pattern made of special characters that helps find text that fits a certain shape or rule, like all words starting with 'a'.
Click to reveal answer
intermediate
How can you make Select-String search case-insensitively?
By default, Select-String is case-insensitive. To make it case-sensitive, add the -CaseSensitive switch set to $true. The -NotMatch switch is used for inverse matching.
Click to reveal answer
beginner
What does the output of Select-String include when it finds a match?
It shows the file name, the line number, and the text of the line where the match was found.
Click to reveal answer
Which cmdlet is used to search text using regex in PowerShell?
ASearch-File
BGet-Content
CFind-Text
DSelect-String
What does the pattern ^a match in Select-String?
ALines ending with 'a'
BLines starting with 'a'
CAny 'a' in the line
DLines containing the word 'a'
How do you search multiple files with Select-String?
AUse wildcards in the -Path parameter
BRun the command multiple times
CUse the -FileList parameter
DIt cannot search multiple files
What switch makes Select-String return lines that do NOT match the pattern?
A-NoMatch
B-Exclude
C-NotMatch
D-InvertMatch
If you want to find the word 'cat' regardless of case, which command is correct?
ASelect-String -Pattern 'cat'
BSelect-String -Pattern 'cat' -CaseSensitive
CSelect-String -Pattern 'cat' -CaseSensitive:`$false
DSelect-String -Pattern 'CAT' -CaseSensitive
Explain how you would use Select-String to find all lines containing a phone number pattern in a text file.
Think about how phone numbers look and how regex can match digits and separators.
You got /3 concepts.
    Describe the output you get from Select-String when it finds matches and how you can use that output.
    Imagine you are looking for a word in many files and want to know exactly where it is.
    You got /4 concepts.