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.

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

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!

  • This looks like what I need. How do I write the results to a .csv file? Thanks – Aslan Jul 17 '14 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" – Jeff Freeman Aug 12 '14 at 15:13
  • how do we use this code in powershell v2.0? – Aslan Jan 28 '15 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 Aug 6 '15 at 23:26
  • 1
    @Underverse: the second Invoke-WebRequest uses "-Url" instead of "-Uri" -- a typo. – jmoreno Jul 20 '16 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.

  • Thank you for your suggestion. Would this work for password protected pages as well? – Aslan Jul 17 '14 at 19:44
  • Such tools accept in many cases credentials. You have to check though. – yandr Jul 26 '14 at 5:36
  • Some sites disallow third party software which means that using utilities like this not an option – Underverse Mar 31 '16 at 3:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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