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.

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

Enjoy.

Leave a Reply

Your email address will not be published. Required fields are marked *