Okrx
Okrx

Reputation: 119

Select-String does not work on log files

I have some logs where I want to select some strings. I tried this on some tests notepad.txt files:

gci | Select-String -Pattern "Deleting" | group path | select name 

and it worked perfectly fine. Now I want to do the same for our *.log file but nothing shows up on a screen. I can open a log file in notepad without problems. I noticed that if I run:

Get-Content myLog.log

it returns:

0 0 : 0 1 : 3 9 . 0 5 3 [ I N T E R N A L . T ( 3 5 4 ) ] >   D e l e t i n g   f i l e   ' \ \ l e v i a t a n \ b a i x e s \ O F F S U M A R I D A

What possibly can be wrong with these logs file that makes get-content to add so many spaces? Is there a way in PowerShell to remove all spaces? Is there an alternative?

Upvotes: 0

Views: 255

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200193

Looks to me like your log file is UTF-16 encoded without a byte order mark (BOM). Both Select-String and Get-Content default to ASCII/ANSI encoding unless a BOM indicates otherwise. You can override the detected/default encoding via the parameter -Encoding:

Get-ChildItem | Select-String -Pattern "Deleting" -Encoding unicode | ...

Upvotes: 2

Related Questions