You could try below PowerShell to download files with NoAttachment metadata as false.
# Load the SharePoint Client Object Model
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
# Define variables
$siteUrl = "https://yoursharepointsiteurl"
$libraryName = "Documents" # The name of your document library
$downloadFolder = "C:\Downloads\Files" # Local path where the files will be downloaded
$metadataField = "NoAttachment" # Metadata field name
# CAML Query to filter files based on metadata (NoAttachment = False)
$camlQuery = @"
<View>
<Query>
<Where>
<Eq>
<FieldRef Name='$metadataField' />
<Value Type='Boolean'>0</Value> <!-- Only files where NoAttachment is False -->
</Eq>
</Where>
</Query>
</View>
"@
# Connect to the SharePoint site using Client Context
$context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
# Provide credentials
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials("******@domain.com", (ConvertTo-SecureString "yourpassword" -AsPlainText -Force))
# Get the document library
$library = $context.Web.Lists.GetByTitle($libraryName)
# Create a new CamlQuery object and execute it
$query = New-Object Microsoft.SharePoint.Client.CamlQuery
$query.ViewXml = $camlQuery
# Get the items matching the query
$items = $library.GetItems($query)
$context.Load($items)
$context.ExecuteQuery()
# Loop through each item and download the files (attachments)
foreach ($item in $items) {
# Check if the item has attachments
if ($item.Attachments.Count -gt 0) {
Write-Host "Item with ID $($item.Id) has attachments. Downloading..."
# Get the attachment files
foreach ($attachment in $item.Attachments) {
$attachmentUrl = $attachment.Url
$fileName = [System.IO.Path]::GetFileName($attachmentUrl)
$fileUrl = "$siteUrl$attachmentUrl"
# Download the file
$file = Invoke-WebRequest -Uri $fileUrl -OutFile "$downloadFolder\$fileName"
Write-Host "Downloaded attachment: $fileName"
}
} else {
Write-Host "Item with ID $($item.Id) has no attachments."
}
}
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.