Simple PowerShell snippet to search file or array for content.

April 22, 2015

Since I play these days a lot with PowerShell I need from time to time some simple functionality. Sometimes dead simple. Like today , snippet to search file content or array of data if you will and return true/false if text is present in it.

function File-Contains-Row {
    [CmdletBinding()]
    Param([array]$fileContent, [string]$searchedString)

    Process {
    #is there any string we search for in any of the rows?
    [array]$resArray = $fileContent | where { $_ | Select-String –caseSensitive -Pattern $searchedString }

    if ($resArray.Count -eq 0) {
        return $false
    } else {
        return $true
    }
    }
}

As a project, you can find it here : https://github.com/rostacik/PSSearchFileOrArrayForContent

.ps1 file is here : https://github.com/rostacik/PSSearchFileOrArrayForContent/blob/master/PSSearchFileOrArrayForContent/Array-Contains-Row.ps1

Hope this helps.


Profile picture

Written by Dušan Roštár - the "mr edge case" guy
my twitter : rostacik, my linkedin : rostar, drop me an email : here