Replace Text within multiple Wikipages using PowerShell


 Script for Sharepoint 2010 Enterprise Wikis!

$logfile = "c:\logfile.txt"
$web = Get-SPWeb "http://intranet/wikiurl"
$list = $web.GetList(($web.ServerRelativeUrl.TrimEnd("/") + "/Wiki%20Pages"))

$search = "search_text"
$replace = "replace_test"

(get-date).ToString() + " >>> Script Startet >> Parameter: Search = " + $search + " | Replace = "+$replace   | add-content $logfile
(get-date).ToString() + " >>> Script Startet >> Parameter: Search = " + $search + " | Replace = "+$replace   | add-content $logfile
foreach ($item in $list.items)
{

if ($item["Page Content"].contains($search))
{

$item.file.CheckOut();

(get-date).ToString() + " >>> " +  $item.name + " checked out"   | add-content $logfile

do {write-host -NoNewline .;Start-Sleep -m 10;} while ($item.file.CheckOutStatus -eq "None")


 

$item["Page Content"] = $item["Page Content"].replace($search ,$replace );

$item.update();

(get-date).ToString() + " >>> " +  $item.name + " modified"   | add-content $logfile

(get-date).ToString() + " >>> " +  $search + " changed by " + $replace  | add-content $logfile

sleep 1

$item.file.CheckIn("checked in by administrator");

(get-date).ToString() + " >>> " +  $item.name + " checked in"   | add-content $logfile


 

write-host $item.name "modified" -foregroundcolor red

}

}


 

_________________________________________________________________________________

Script for migrated SharePoint 2007 Wikis to SP 2010!


 

["Page Content"] has been replaced by ["ows_WikiField"]


 


 


 

$logfile = "c:\logfile.txt"

$web = Get-SPWeb "http://intranet/wikiurl"

$list = $web.GetList(($web.ServerRelativeUrl.TrimEnd("/") + "/Wiki%20Pages"))


 

$search = "search_text"

$replace = "replace_test"


 


 

(get-date).ToString() + " >>> Script Startet >> Parameter: Search = " + $search + " | Replace = "+$replace   | add-content $logfile

(get-date).ToString() + " >>> Script Startet >> Parameter: Search = " + $search + " | Replace = "+$replace   | add-content $logfile

foreach ($item in $list.items)

{


 

 if ($item["ows_WikiField"].contains($search))

 {

  $item.file.CheckOut();

  (get-date).ToString() + " >>> " +  $item.name + " checked out"   | add-content $logfile

  do {write-host -NoNewline .;Start-Sleep -m 10;} while ($item.file.CheckOutStatus -eq "None")


 

  $item["ows_WikiField"] = $item["ows_WikiField"].replace($search ,$replace );

  $item.update();

  (get-date).ToString() + " >>> " +  $item.name + " modified"   | add-content $logfile

  (get-date).ToString() + " >>> " +  $search + " changed by " + $replace  | add-content $logfile

  sleep 1

  $item.file.CheckIn("checked in by administrator");

  (get-date).ToString() + " >>> " +  $item.name + " checked in"   | add-content $logfile


 

  write-host $item.name "modified" -foregroundcolor red

 }

}

  


Comments (10)
  1. Ольга добрый день!Мне интересно цены на еду Если сам готовишь? И алкоголь на острове панглао

  2. davisonline11 says:

    For SharePoint 2013 Enterprise WIki, use $item["Wiki Content"] instead of "Page Content". It works great! Thanks!

  3. rm says:

    The script looks good, but I keep getting below error …

    You cannot call a method on a null-valued expression.
    At C:UsersSPSFarmAdminDesktopfindNReplace.ps1:25 char:7
    + if ($item["Page Content"].contains($search))
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

    You cannot call a method on a null-valued expression.
    At C:UsersSPSFarmAdminDesktopfindNReplace.ps1:25 char:7
    + if ($item["Page Content"].contains($search))
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

  4. BP says:

    Finally answered my question. When you attempt to update the link in WikiField, SharePoint will validate the link. If it is invalid, it gets stripped. If you try to do a relative link like "wikipage.aspx" it will be considered invalid. Instead you would
    at least need "/wikipage.aspx" or "/subsite/list/wikipage.aspx" to be considered valid.

    Hope this helps others!

  5. BP says:

    I was trying to update the links in SharePoint 2013 within the wiki pages changing it from /some/old/location/wikipage.aspx to just wikipage.aspx. Everything works until the Update. For some reason, item.Update() removes the href in a link after it runs.

    I really don’t know why this happens and I’ve found nothing referencing that online.

  6. Eric says:

    this is great to just change text on the page!
    What I really need, is to change the url of a link on a wiki page.

  7. brian brooks says:

    Hi Thomas, script looks awesome but I’m having trouble referencing Get-SPWeb. Where can I get the PowerShell CmdLet Get-SPWeb? I’m not the SharePoint administrator, I just a user of our corporate SharePoint.

  8. johnsonraj says:

    Nice post ,thanks for sharing this information .

    <a href="http://www.raybiztech.com/Sharepoint-Portal-Solutions.html">Sharepoint Developers</a>

  9. Anonymous says:

    For other readers, if you just want to search and replace on a single page, edit the page in a non-Internet Explorer browser. Copy the text of the page to a text editor. Do the search and replace in the text editor. Then paste the updated text back in
    to the browser and save.

  10. to run this script, you have to be a sharepoint farm admin.
    otherwhise you cannot run sharepoint specific powershell commands.

Comments are closed.

Skip to main content