There are probably many ways to do this but I spotted this question in the Powershell forums and thought I’d share.

“Is there a way to restrict the output from “SELECT-STRING” to not show the Filename or the line number?

For those of you like me, still learning; SELECT-STRING allows you to scan a FILE or a SERIES of files for matching Content. 

In it’s most basic form it works like this

SELECT-STRING –path FILEPATHFILENAME –pattern ‘STUFFIAMLOOKINGFOR’

In the UNIX world I believe it was “GREPPING” 🙂

Anyhow normally if you do a SELECT-STRING on a file called say “DOG.TXT” for any reference to “CAT” you would get output like this

SELECT-STRING –path DOG.TXT –pattern ‘CAT’

dog.txt:1:This is a mean ugly cat
dog.txt:5:Cat is going around dog in Circles
dog.txt:93:Dog is laughing at soaking wet Cat

Yes a silly example but if you just wanted the lines (Say this was a security log? Say you just wanted parse out and build a new file with only specific content from the original?  Those line numbers can be irritating! and USELESS.

So to clean it up, just use a FOREACH and pull out only what we need.  A GET-MEMBER against the SELECT-STRING does as so

SELECT-STRING –path DOG.TXT –pattern ‘CAT’ | GET-MEMBER

Shows us the following results

image

The actual Property we’re looking for is just called “LINE”, it has the content without the Numbers.  It you run the SELECT-STRING like THIS

SELECT-STRING –path DOG.TXT –pattern ‘CAT’ | GET-MEMBER | FOREACH { $-.Line }

You get just the lines Output.  Also in Powershell, if you’re curious about output, just use a FOREACH { $-.Property } on the end of a line to read the actual content of Properties to see if it’s what you need.

Keep on Scripting!

Sean
The Energized Tech