Simple PowerShell snippet to search file or array for content

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.

[powershell]
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
}
}
}
[/powershell]

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

Enjoy.

0 comments

This site uses Akismet to reduce spam. Learn how your comment data is processed.