# Find and replace text in each pipeline string. Omit the -Replace parameter to delete # text instead. Use the -SimpleMatch switch to work with literal text instead of regular # expressions. Comparisons are case-sensitive unless the -IgnoreCase switch is used. Filter Edit-String { Param([string]$Find, [string]$Replace='', [switch]$SimpleMatch, [switch]$IgnoreCase) if ($SimpleMatch) { if ($IgnoreCase) { return $_.Replace($Find, $Replace, [System.StringComparison]::OrdinalIgnoreCase) } return $_.Replace($Find, $Replace) } if ($IgnoreCase) { return $_ -replace $Find, $Replace } return $_ -creplace $Find, $Replace } Set-Alias replace Edit-String Set-Alias sc Set-Content Save Usage # 1 file $f = a.txt; gc $f | replace '[MYID]' 'MyValue' -SimpleMatch | sc $f # 0 to many files gci *.txt | % { gc $_ | replace '\[MYID\]' 'MyValue' | sc $_ } # Several replacements chained together ... | replace '[1-9]' T | replace a b -IgnoreCase | replace 'delete me' | ... # Alias cheat sheet # gci Get-ChildItem # gc Get-Content # sc Set-Conent # % ForEach-Object
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter