I'm using this script to copy files from SharePoint list to document library, I'm planning to schedule this everyday but the problem is its copying existing files and created duplicates. Is there a way to exclude copying existing files in document library?
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$WebURL = "http://intranet/sites/test/"
$SourceListName = "agreement_list"
$TargetLibraryName = "agreement"
Get the Web List and Library objects
$web = Get-SPWeb $WebURL
$SourceList = $web.Lists[$SourceListName]
$TargetLibrary = $web.Lists[$TargetLibraryName]
Loop through each list item
foreach ($ListItem in $SourceList.Items)
if($ListItem.Attachments.Count -gt 0)
Loop through each attachment in the list item
foreach ($Attachment in $ListItem.Attachments)
Get the attachment
$file = $web.GetFile($ListItem.Attachments.UrlPrefix+$Attachment)
$bytes = $file.OpenBinary()
$TargetFileName = $TargetLibrary.RootFolder.Url+"/"+$Attachment
$TargetFile = $TargetLibrary.RootFolder.Files.Add($TargetFileName, $bytes, $true)
Write-Host "Copied to: $($TargetFilename)"