DROTHISM

Searching a Text File in PowerShell

Posted by Drothism on Sep 18th, 2008

If you are looking for a quick and simple way to search a text file for a certain string PowerShell has a very efficient method called Select-String. In its simplest form just use the following format…

if (select-string -pattern “findme” -path “D:\Scripts\test.txt”)
{
    Write-Host “Found It!”
}

In the case above I am searching the text file “text.txt” for the text “findme”. The result is “Found It!” if the text exists in the file.

Leave a reply

You must be logged in to post a comment.