SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have some files in SP 2010 that are simply HTML, and I need to modify the contents of those files in a systematic manner.

Is there a good way to do this using powershell? Imagine that I want to find certain tags or text and replace them with something else, like replace "p" with "P".

share|improve this question
up vote 7 down vote accepted

There's probably a bunch of different ways to do this, but here's a snippet from some C# code that I wrote a while back converted to PowerShell. Hope it's along the lines of what you're looking for:

Start-SPAssignment -Global
$web = Get-SPWeb "http://sharepointdev:90"
$list = $web.Lists.TryGetList("Site Pages")
foreach ($file in $list.RootFolder.Files) 
    if ($list.ForceCheckout) { $file.CheckOut() } 
    $html = [System.Text.Encoding]::ASCII.GetString($file.OpenBinary())
    $html = $html.Replace("p", "P")
    if ($list.ForceCheckout) { $file.CheckIn() }
Stop-SPAssignment -Global
share|improve this answer
TIL about the Start-SPAssignment -Global. Neat... – tarjeieo Aug 2 '11 at 10:44
your replace statement would probably want to include "<p>","<P>" if you want to replace the tags. – Shawn Melton Aug 2 '11 at 13:15
The original question said "tags or text" and I just used his example for context. – Rob D'Oria Aug 2 '11 at 13:22
Thank you, this is a great example, got me pointed where I needed to go. – Daniel Williams Aug 3 '11 at 16:33

Since you're mentioning HTML files, you could also use the SPWeb.GetFileAsString() method to get the contents of the file and SPWeb.Files.Add(string, byte[], bool) to save the contents.

share|improve this answer

I came across this PowerShell function recently and thought it could be helpful for situations like the one you mention. I haven't actually used it yet, but it could be another option for you.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.