I have this on-premises power shell script which reads SharePoint list attachments and save them to local folder:

  $ItemAttachmentURLPrefix =$HeaderListItem.Attachments.UrlPrefix
    $attachmentCollection = $HeaderListItem.Attachments

    $AttachmentSeq = 0
    ForEach($Attachment in $attachmentCollection)
        Write-Host "`tDownloading Attachement: " $attachment $ItemAttachmentURLPrefix

        $AttachmentDataObj = "" | Select "Attachment Seq", "JDE Company", "Vendor Number", "Vendor Invoice Number", "Attachment Name", "Attachment System Name", "Job-run Date/Time Stamp"

        $file = $spSourceWeb.GetFile($ItemAttachmentURLPrefix + $attachment)
        $bytes = $file.OpenBinary() 
        $AttachmentFileName = $FilePrefix + "_AP_" + $InvoiceIDFormatted + "_" + $attachment
        $Attachmentpath = $AttachmentFolder + $AttachmentFileName #$FilePrefix + "_*emphasized text*AP_" + $InvoiceIDFormatted + "_" + $attachment
        $fs = new-object System.IO.FileStream($Attachmentpath, "OpenOrCreate") 
        $fs.Write($bytes, 0 , $bytes.Length)    


But how can I convert this to PnP PowerShell?

1 Answer 1


Try using below PnP PowerShell code to download list item attachments:

Connect-PnPOnline -Url https://contoso.sharepoint.com/sites/dev -Interactive

$listitem = Get-PnPListItem -List Employee -Id 2  
$attachments = ForEach-Object {Get-PnPProperty -ClientObject $listitem -Property "AttachmentFiles"}  

$attachments | ForEach-Object { Get-PnPFile -Url $_.ServerRelativeUrl -FileName $_.FileName -Path "E:\SharePoint\Documents" -AsFile }

Source: Download Attachments From SharePoint List Item Using PnP PowerShell

  • thanks ,, but how i can mimic $fs = new-object System.IO.FileStream($Attachmentpath, "OpenOrCreate") ? Thanks
    – John John
    Dec 9, 2022 at 8:59
  • 1
    I don't think you need to use that. Above code should for you as it is. Dec 9, 2022 at 9:18

Your Answer

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

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