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!
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.
-
-
1K.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
-
-
1
-
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