Using select-string to find occurrences of a string

Hello Scripting Guys,

I'm unsuccessfully trying to use the select-string cmdlet to find all occurrences of the string "Number of disposed messages : " in a log directory. I can use select-string to find all occurrences of ":" in the same log folder.

Why can't the select-string cmdlet find my string?

Sincerely,

Brian Street.

p.s. I am able to use the PowerShell example # 3: PS C:\> select-string -path $pshome\en-US\*.txt -pattern "@"  I'm also able to search within the log folder for the occurrence of ":" in the log files.

August 20th, 2015 2:04pm

Hi Brian,

What command are you actually running? This should work just fine:

PS C:\Scripts\PowerShell Scripts\Misc Testing\8-20-2015> Select-String -Path *.txt -Pattern 'Number of disposed messages :'

1.txt:3:Number of disposed messages : file 1
2.txt:3:Number of disposed messages : file 2
4.txt:3:Number of disposed messages : file 4


1.txt:
blah : 1
blah : 2
Number of disposed messages : file 1
blah : 3

2.txt:
blah : 1
blah : 2
Number of disposed messages : file 2
blah : 3

3.txt:
blah : 1
blah : 2
blah : 3

4.txt
blah : 1
blah : 2
Number of disposed messages : file 4
blah : 3

Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 2:13pm

Thanks for the response.

I'm running the following command: select-string -Path *.txt -Pattern "Number of disposed messages :" from within the log folder.  I can search within the same folder for occurrences of ":" but as soon as I add any words inside the double-quotes, it fails to return any results.


August 20th, 2015 2:20pm

Interesting. I can't replicate your issue, as the command I used is exactly the same as yours (I used single quotes, but doubles do work).

Perhaps there is something odd with your input files. Try using my small test files and see if the issue continues.

Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 2:27pm

Thanks Mike.

ok; that definitely confirms there is something fishy with the log files I'm searching.


August 20th, 2015 2:37pm

update:

the log files were saved in Unicode format. when I saved a log file as ANSI format I was able to search the file for the occurrence of the string.

Adding -Encoding Unicode to the cmdlet resolved the issue.
Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 3:06pm

update:

the log files were saved in Unicode format. when I saved a log file as ANSI format I was able to search the file for the occurrence of the string.

In that case you can try using Select-String's -Encoding parameter:

https://technet.microsoft.com/en-us/library/hh849903%28v=wps.630%29.aspx

August 20th, 2015 3:09pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics