Posts for 02nd February 2012

A recent project has been to change the case of portions of text within a set of files.

This can be achieved by the following script

foreach ($file in Get-ChildItem *.php) 
    (Get-Content $file) | 
        [Regex]::Replace($_, 'href=".+?"', {param($m) $m.Value.toLower()}) 
    } | 
    Set-content $file

Two items to note

  • The Get-Content cmdlet is bracketed to ensure the file is finished with before the Set-Content cmdlet is called.
  • By using PowerShell 2 it is possible to pass script blocks as a delegate. Consequently entries that match the RegEx pattern can be processed, in this case converted to their lower case equivalent