Share via


find word in a text file and return boolean

Question

Friday, January 24, 2014 6:55 PM

Hi,

How do I search for a "keyword" in a text file, if keyword found i just want to have returned $true, and if not found return $false. Please suggest me anything other than .contains method because that's complaining and giving error when I am running my ps scripts from a scheduler called Visualcron

All replies (2)

Friday, January 24, 2014 7:03 PM âś…Answered | 2 votes

Select-String can do that for you:

$file = '.\someFile.txt'
$word = 'Xylophone'

$found = Select-String -Path $file -Pattern $word -SimpleMatch -Quiet

$found will be a boolean value.


Monday, January 27, 2014 2:56 AM | 1 vote

Hi,

Please also try below code:

$content = get-content e:\test.txt

$word = "test"

If($content -match $word)

{$true}

else

{$false}

Regards,

Yan Li

Regards, Yan Li