2

I need a SharePoint 2010 Management Shell script that is going to generate a csv file with a list of broken links in one of my site collection and its sub sites. The script should support powershell version 2.0

I have no experience in writing PS scripts. Any help would be highly appreciated.

1
  • Use SharePoint Broken Link Manager, it was written specifically for SharePoint broken links qipoint.com
    – user47120
    Commented Sep 18, 2015 at 23:10

2 Answers 2

9

Here's a PowerShell function that returns a hash table with all links on a page at a given URL and their status codes:

Function Get-LinksWithStatusCode
(
    [string] $Url
)
{
    $links = (Invoke-WebRequest -Uri $Url).Links | Select -ExpandProperty href -Unique
    $hash = @{}

    foreach ($link in $links)
    {
        Try
        {
            $statusCode = Invoke-WebRequest -Url $link -MaximumRedirection 0 -ErrorAction SilentlyContinue | Select -ExpandProperty StatusCode
        }
        Catch
        {
            $_
            $statusCode = $_
        }

        $hash.Add($link, $statusCode)       
    }
}

I reappropriated a lot of the code from this link; it requires PowerShell v3.0. If you cannot use v3, you can use [System.Net.WebClient].

Also, I didn't filter out the links section to only http://* like the blog author did. If you do this, you lose all internal links in SharePoint. I'm not sure you can execute those Invoke-WebRequests, so you may need to append your SPWeb.Url onto those. Hope this helps!

9
  • This looks like what I need. How do I write the results to a .csv file? Thanks
    – Aslan
    Commented Jul 17, 2014 at 19:43
  • Sorry that I didn't see your comment sooner. To write the results to a csv, try this $hash | Export-Csv "filename.csv" Commented Aug 12, 2014 at 15:13
  • how do we use this code in powershell v2.0?
    – Aslan
    Commented Jan 28, 2015 at 12:40
  • 1
    Instead of $statusCode = $_ you should be using $_.Exception.Response.StatusCode.Value__ -- that way all of the values are the actual response codes (I'd also suggest not echoing out the exception as it is pretty pointless, since you are adding them all to the list).
    – jmoreno
    Commented Aug 6, 2015 at 23:26
  • 1
    @Underverse: the second Invoke-WebRequest uses "-Url" instead of "-Uri" -- a typo.
    – jmoreno
    Commented Jul 20, 2016 at 1:55
0

Why do you need PowerShell for that?

Just use any tool for identifying broken links, such as Xenu's Link Sleuth

You don't need to run from the server either, plus it provides more information and a GUI.

Of course you can search for other similar tools if this is not the best choice for you.

4
  • Thank you for your suggestion. Would this work for password protected pages as well?
    – Aslan
    Commented Jul 17, 2014 at 19:44
  • Such tools accept in many cases credentials. You have to check though.
    – yandr
    Commented Jul 26, 2014 at 5:36
  • Some sites disallow third party software which means that using utilities like this not an option
    – Underverse
    Commented Mar 31, 2016 at 3:52
  • 1
    The benefit of using PowerShell is you're not introducing a 3rd party program into the environment and thus no change to the security posture.
    – phbits
    Commented Sep 23, 2021 at 18:00

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.