PoshCode Archive  Artifact [7d31bb1680]

Artifact 7d31bb16804956fecd712af8b32f6f50cf21bb90930496ea48f12d99f87415db:

  • File Find-String.ps1 — part of check-in [fd29f8446b] at 2018-06-10 12:56:25 on branch trunk — Yet another find text in text files, this time with context, too. (user: unknown size: 1206)

# 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
# license: CC0
# x-poshcode-id: 1096
# x-archived: 2009-10-25T15:24:51
#
#
<#
.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