Below I have a ps1 for finding and appending text defined by regex pattern; i.e. from [pattern] to [pattern]Foo. Is there a simpler way to do this for multiple regex patterns, other than defining each regex as pattern2, pattern3, etc. and creating a separate "ForEach" to correspond to every regex? Because that's how I did it, and it works but it looks very rudimentary.
$pattern1 = [regex]'([___)'
$pattern2 = [regex]'([___)'
Get-ChildItem 'C:\\File\\Location\\*.txt' -Recurse | ForEach {
(Get-Content $_ |
ForEach { $_ -replace $pattern1, ('$1'+'FOO')} |
ForEach { $_ -replace $pattern2, ('$1'+'FOO')}) |
Set-Content $_
}
[regex]'(pattern1|pattern2)'[needs an escape\[otherwise it's an invalid pattern. Also, use-rawparameter in Get-Content if you're on PowerShell 3 or newer to make processing much faster.-replacesuch asGet-Content $_ -replace $pattern1, '$1FOO' -replace $pattern2, '$1FOO'