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 Jul 19, 2016 at 11:00
  • It's not giving any result when I added $list.Name -eq "Test" to the if condition.
    – abc
    Jul 19, 2016 at 12:32
  • should you send to me the if statement after your modification ? 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”)
    – abc
    Jul 19, 2016 at 12:48
  • 1
    I updated the answer above $list.Title instead of $list.Name 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, privacy policy and cookie policy

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