SharePoint keeps attachments under "List >> Attachments >> 'List Item ID'" folder path. So, if you want to copy attachment from list to document library, follow these steps:
PowerShell script to copy attachment from list to document library:
- Open your SharePoint site from SharePoint Designer
- Navigate to "All Files" view >> Lists >> Your Source List >> Attachments folder. Here, folders are created based on list item's ID.
- Just copy attachment files from these folders and navigate to the target document library and paste there.
PowerShell script to copy attachment from list to document library:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinueThis script copies all attachments from all list items to the given library's root folder. You can tweak the script little to create sub-folders in the target library too.
$WebURL = "http://sharepoint.crescent.com/pmo/GIS/"
$SourceListName = "External Projects"
$TargetLibraryName = "Design Documents"
#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)"
}
}
}