Skip to content

Latest commit

 

History

History
47 lines (37 loc) · 1.21 KB

File metadata and controls

47 lines (37 loc) · 1.21 KB
pid 1096
author David Mohundro
title Find-String
date 2009-05-12 05:47:49 -0700
format posh
parent 0

Find-String

Yet another find text in text files, this time with context, too.

<#
.Synopsis
	Searches text files by pattern and displays the results.
.Description
	Searches text files by pattern and displays the results.
.Notes
Based on versions from http://weblogs.asp.net/whaggard/archive/2007/03/23/powershell-script-to-find-strings-and-highlight-them-in-the-output.aspx and from http://poshcode.org/426

Makes use of Out-ColorMatchInfo found at http://poshcode.org/1095.
#>

#requires -version 2
param ( 
	[Parameter(Mandatory=$true)] 
	[regex] $pattern,
	[string] $filter = "*.*",
	[switch] $recurse = $true,
	[switch] $caseSensitive = $false,
	[int[]] $context = 0
)

if ((-not $caseSensitive) -and (-not $pattern.Options -match "IgnoreCase")) {
	$pattern = New-Object regex $pattern.ToString(),@($pattern.Options,"IgnoreCase")
}

Get-ChildItem -recurse:$recurse -filter:$filter |
	Select-String -caseSensitive:$caseSensitive -pattern:$pattern -AllMatches -context $context | 
	Out-ColorMatchInfo