I'm hoping that someone can help me get started on a script that will crawl our Wiki Pages for Wiki Links that point to the same page the link is located on. For example, if I have a Wiki Page called "Testing" that has the Wiki Link [[Testing]] in its contents, I want to remove it. It is my stance that pages should not have links in them that point to the page the user is already on, but some of our Wiki users do not seem to have this same stance. Haha.

Any advice or direction would be intensely appreciated. Thank you.

  • By "script", I'm assuming you want a PowerShell script. Is this for on-prem SharePoint or SharePoint online? Commented Sep 22, 2018 at 19:55
  • Hi Mike. It is on-prem.
    – C. Dennett
    Commented Sep 24, 2018 at 22:35

1 Answer 1


You mean like... "Recursion", see "Recursion"? ;-)

For an on-prem SharePoint for one wiki:

$web = get-spweb http://yourServer/sites/yourSite
$wiki = $web.Lists["Wiki"]
$wiki.items | 
  foreach { $page = $_.Name; $name = $_.DisplayName; $_ } | 
  foreach { ([regex]'id="\d*::\w*\|').Matches($_.xml) } | 
  foreach {$_.Value.split(';')[5].split('|')[0] } | 
  where {$_ -eq $name } | 
  select {$page} -Unique

This uses a Regular Expression to match the link HTML found in the item's XML property. The wiki link text is found and matched to the item's DisplayName property.

This is the text it is matching:

<p>See&#58; <a id="0&#58;&#58;Recursion|Recursion";

Here's a version that will work with SharePoint on-prem or SharePoint Online. You will need to download and install the SharePoint PNP cmdlets. (https://docs.microsoft.com/en-us/powershell/sharepoint/sharepoint-pnp/sharepoint-pnp-cmdlets?view=sharepoint-ps)

$URL = "http://yourServer/sites/yourSite"
$wikiname = "Wiki"
$wikipath = "/sites/yourSite/wiki/"
Connect-PnPOnline $url
Get-PnPListItem -List $wikiname -Fields FileLeafRef |
  foreach { $pagename = $_["FileLeafRef"]; $_ } |
  Get-PnPWikiPageContent -ServerRelativePageUrl {$wikipath+ $pagename} |
  where { $_ -like "*$pagename*" } |
  select {$pagename}
  • Thank you very much for your answer, Mike. I will try this, and mark as answer if it works!
    – C. Dennett
    Commented Sep 24, 2018 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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