Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

being both quite new to both PowerShell and SharePoint 2010 I'm wondering if there is any way to make a script for searching a lists for links that begins in a certain way? I want to find all the links that starts with "http", "https" or "file//" and since I'm not quite up to speed with neither PS or SP2010 I felt it was better to ask before I started doing anything. Thx for any help or pointers, even smug remarks are appreciated if they in some way help out!

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

If I understand you correctly, by 'searching a list for links' you actually need to search list for HyperLink fields?

If that is true here is simple PS script that search all list in given web for links starting by "http", "https" or "file":

$web = Get-SPWeb "http://mycoolsite"
$result = @() #Edited
# Iterate lists
foreach ($list in $web.Lists) {
    # First find all HyperLink fields in list
    $fields = @()
    foreach ($field in $list.Fields) {
        if ($field.TypeAsString -eq "URL") {
            $fields = $fields + $field.Title        
        }
    }

    # Skip list because there are no Hyperlink fields
    if($fields.Count -eq 0) { continue }
    # Iterate list items NOTE: this can be slow
    foreach ($item in $list.Items) {
        # Iterate HyperLink fields
        foreach ($field in $fields) {
            # Get field value
            $fldValue = $item[$field]
            # Skip if null
            if($fldValue -ne $null) { 
                # Check for http: https: or file:
                if($fldValue.Contains("http://") -or  $fldValue.Contains("https://") -or $fldValue.Contains("file://")) {
                    $result += $list.Title + " " +  $item["ID"] + " " + $fldValue #Edited 
                }
            }
        }
    }
}
$result | Out-File file_links.txt #Edited

Edited:

I edited my original code to answer additional question posted in comments (how to save results as txt file?). I made minor adjustments and marked them with #Edited. Please note this is not only option to export results but it is one that required minimal changes in original script.

share|improve this answer
    
This is exactly what I was looking for, huge thanks!!! –  K.W Sep 13 '11 at 13:37
1  
K.W. - This is a solution that should work great for your needs, but just be aware of Vedran's comment in the code. Iterating over so many list items can be very slow performing if you have a large collection of lists. –  Tim Gabrhel Sep 13 '11 at 13:47
    
Does $web need to be disposed of in this case? –  Tim Gabrhel Sep 13 '11 at 13:47
1  
@Tim - look at this: stackoverflow.com/questions/1999858/… –  Vedran Rasol Sep 13 '11 at 14:01
    
Thanks! I was working with what the answer said yesterday. Ad-hoc scripts that were not being disposed. –  Tim Gabrhel Sep 13 '11 at 14:10
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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