1

I'm trying to look through all files in a path including any sub-folders on a network share. I'm looking for a specific string within the files. The string is set in $searchPattern and the starting directory is under $path.

I know there's at least one result that it should find and it returns nothing. The search string has to include the newline character so I included that in the variable. I'm looking for help as to why I may not be getting what I expect? I'm relatively new to PS1 scripting.

For what its worth, I think it has to do with the newline. When I remove the `n from the searchPattern variable, I do get results back... but what I'm trying to accomplish is find any records where there's no printer set and a sample of that is included below.

Thank you!

$path = '\\printershare\printers\'
$searchPattern = 'label_printer=`n' 
# note the newline char at the end of the pattern

Get-ChildItem $path -recurse | Select-String -pattern $searchPattern | group path | select name

sample file that has contents I'm looking for. This file has no label printer set.

[Printers]
local_printer=printer001
local_pharm_printer=printer001
label_printer=
local_nursing_printer=printer001
hosp_pharm_printer=printer001
hosp_nursing_printer=printer001
6
  • Didn't make any difference. Commented Jul 16, 2018 at 18:30
  • 1
    Try this for the pattern ^label_printer=$ Commented Jul 16, 2018 at 18:42
  • That worked. I'll have to look at what that syntax does. Interesting! Thank you :D Toss that in the answer for points. Commented Jul 16, 2018 at 18:43
  • @Frantumn ^ means start of string, $ means end of string. Commented Jul 16, 2018 at 19:37
  • 2
    Apart from @EBGreen's answer, which is right, Windows newlines are CRLF not just LF, so you need a backtick-r then backtick-n. And you can't put escape characters in a single-quoted string in PowerShell, only in a double-quoted string. But then, this approach doesn't work even if you do fix those, as described in EBGreen's answer. But those are things to note anyway. Commented Jul 17, 2018 at 2:02

1 Answer 1

3

When you are using Select-String on a group of files like that it is essentially taking each file and splitting the lines of that file into an array. Then it looks at each line and compares it to the regex pattern. In this case:

^label_printer=$

^ indicates the beginning of the string being looked at.

label_printer= means find the literal string indicated.

$ indicates the end of the string being looked at.

Since Select-String was effectively splitting the files on carriage returns, none of the strings you were looking at had carriage returns in them to match.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.