Replace Text within multiple Wikipages using PowerShell
Script for Sharepoint 2010 Enterprise Wikis!
$logfile = "c:\logfile.txt"
$web = Get-SPWeb "https://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 "https://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
Anonymous
January 01, 2003
to run this script, you have to be a sharepoint farm admin.
otherwhise you cannot run sharepoint specific powershell commands.Anonymous
January 01, 2003
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.Anonymous
June 05, 2013
Nice post ,thanks for sharing this information . <a href="www.raybiztech.com/Sharepoint-Portal-Solutions.html">Sharepoint Developers</a>Anonymous
August 06, 2014
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.Anonymous
January 28, 2015
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.Anonymous
March 18, 2015
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.Anonymous
March 20, 2015
The comment has been removedAnonymous
August 23, 2015
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 : InvokeMethodOnNullAnonymous
November 12, 2015
For SharePoint 2013 Enterprise WIki, use $item["Wiki Content"] instead of "Page Content". It works great! Thanks!Anonymous
September 07, 2017
Ольга добрый день!Мне интересно цены на еду Если сам готовишь? И алкоголь на острове панглао