Finding string


I know there’s Select-String, and -cmatch to find strings. My understanding is that Select-String is not very strict, and -cmatch is the best option to find exact matches since you can use regex.

My question is, does -cmatch have a .LineNumber option like Select-String?

Thank you,


You can use regex with Select-String as well.

How would I do that?

I have a text file with these numbers, and I’m trying to find only 2=2

$TXT = '\\hqfs1\users\tantony\PowerShell\CalenderGroup\test.txt'
$Read = Get-Content $TXT
$Read | Select-String -CaseSensitive "^2=2$" -SimpleMatch

If I do this, it will not show any results

But, when I have

$TXT = '\\hqfs1\users\tantony\PowerShell\CalenderGroup\test.txt'
$Read = Get-Content $TXT
$Read -cmatch "^2=2$"

It will show only 2=2, which is what I’m looking for

How would I use regex on select-string?

First of all: Did you at least try to get information you need by reading the help for the cmdlet you’re about to use?

Get-Help Select-String -Full
Get-Help Select-String -ShowWindow
Get-Help Select-String -Online

On top of this there are thousands of examples on the internet.
Now to your request:

$TXT = ‘\hqfs1\users\tantony\PowerShell\CalenderGroup\test.txt’
Select-String -Path TXT -Pattern '^2=2’ -AllMatches | Select-Object *

Now you can choose whatever result you are looking for. But actually you don’t need a regex for that. You are looking for a particular string!?

Thank you,

I did look at Get-Help Select-String -Examples