PoshCode Archive  Artifact [c25b81e309]

Artifact c25b81e309f6cc64ffa76e673b47af09c04c374adfb0b311cd3d0964c0e29719:

  • File Find-String.ps1 — part of check-in [da9e50e683] at 2018-06-10 13:08:18 on branch trunk — Yet another find text in text files, this time with context, too. (user: David Mohundro size: 1232)

# encoding: ascii
# api: powershell
# title: Find-String
# description: Yet another find text in text files, this time with context, too.
# version: 0.1
# type: script
# author: David Mohundro
# license: CC0
# x-poshcode-id: 2296
# x-archived: 2010-10-20T09:16:17
#
#
<#
.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