0

I am trying to find the duplicate files in my "Test" library in SharePoint, using the below code

  [system.reflection.assembly]::LoadWithPartialName("Microsoft.SharePoint")
    function Get-DuplicateFiles ($RootSiteUrl)
    {
    $spsite = new-object Microsoft.SharePoint.SPSite($RootSiteUrl)
    $Items = @()
    $Duplicates = @()
    $duplicateItems = @()
    $duplicateshelper = @()
    foreach ($SPweb in $spSite.allwebs)
    {
    Write-Host "Checking " $spWeb.Title " for duplicate documents"
    foreach ($list in $spWeb.Lists)
    {
    if($list.BaseType -eq "DocumentLibrary" -and $list.RootFolder.Url -notlike "_*" -and $list.RootFolder.Url -notlike "SitePages*")
    {
    foreach($item in $list.Items)
    {
    $record = New-Object -TypeName System.Object
    if($item.File.length -gt 0)
    {
      $record | Add-Member NoteProperty FileName ($item.file.Name)
      $record | Add-Member NoteProperty FullPath ($spWeb.Url + "/" + $item.Url)
      $Items += $record
    }
    }
    }
    }
    $spWeb.Dispose()
    $duplicateItems = $Items | Group-Object Filename| Where-Object {$_.Count -gt 1}
    foreach($dup in $duplicateItems)
    {
       foreach($item in $Items | Where-Object {$_.Filename -eq $dup.Name})
      {
        if ($duplicateshelper -notcontains $item.Fullpath) 
        {
           $duplicateshelper += $item.Fullpath
           $found  = New-Object -TypeName System.Object
           $found  | Add-Member NoteProperty Filename ($item.FileName)
           $found  | Add-Member NoteProperty Fullpath ($item.Fullpath)
           $duplicates += $found 
        }
      }
   }
  }
  return $duplicates  | Out-GridView
 }
  Get-DuplicateFiles("http://sp2010/Test")

this giving me result from all the document libraries(Test, SearchCenter, Pages, News) in the site, I want only from "Test" library. Please let me know where to change the code.

2 Answers 2

0

At this foreach specifically at if condition

foreach ($list in $spWeb.Lists)
    {
    if($list.BaseType -eq "DocumentLibrary" -and $list.Title -eq "Test"

add this $list.Title -eq "Test"

The full code should be like this

foreach ($list in $spWeb.Lists)
    {
    if($list.BaseType -eq "DocumentLibrary" -and $list.RootFolder.Url -notlike "_*" -and $list.RootFolder.Url -notlike "SitePages*")
    {

if($item.Title -eq "Test")
{
    foreach($item in $list.Items)
    {
    $record = New-Object -TypeName System.Object
    if($item.File.length -gt 0)
    {
      $record | Add-Member NoteProperty FileName ($item.file.Name)
      $record | Add-Member NoteProperty FullPath ($spWeb.Url + "/" + $item.Url)
      $Items += $record
    }
}
    }
    }
    }

Hope it helps you

6
  • I am also here if you need any clarification or help Commented Jul 19, 2016 at 11:00
  • It's not giving any result when I added $list.Name -eq "Test" to the if condition. Commented Jul 19, 2016 at 12:32
  • should you send to me the if statement after your modification ? Commented Jul 19, 2016 at 12:33
  • Here it is : if($list.BaseType -eq “DocumentLibrary” -and $list.Name -eq "Test" -and $list.RootFolder.Url -notlike “_” -and $list.RootFolder.Url -notlike “SitePages”) Commented Jul 19, 2016 at 12:48
  • 1
    I updated the answer above $list.Title instead of $list.Name Commented Jul 19, 2016 at 14:50
0

If you are sure of your list name, you should probably avoid enumerating manually all the available lists and just accessing the target list directly.

foreach ($SPweb in $spSite.allwebs)
{
   Write-Host "Checking " $spWeb.Title " for duplicate documents"

   $list = $spWeb.Lists.TryGetList("Your List Display Name")

   if(($list) -and ($list.BaseType -eq "DocumentLibrary"))
   {
      foreach($item in $list.Items)
      {
         $record = New-Object -TypeName System.Object
         if($item.File.length -gt 0)
         {
            $record | Add-Member NoteProperty FileName ($item.file.Name)
            $record | Add-Member NoteProperty FullPath ($spWeb.Url + "/" + $item.Url)
            $Items += $record
         }
      }
   }

   // your original code
   // .......

Just replace the "Your List Display Name" part with the actual name of your list - "Test" in your example. The rest of the code should be the same.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.