Save attachments

Welcome Forums General PowerShell Q&A Save attachments

Viewing 3 reply threads
  • Author
    Posts
    • #214908
      Participant
      Topics: 3
      Replies: 21
      Points: 67
      Rank: Member

      Hello, How save attachment? Code return error Method invocation failed because [System.String] doesn’t contain a method named ‘saveasfile’. My code is
      $outlook = new-object -com outlook.application;

      $namespace = $outlook.GetNameSpace(“MAPI”);

      $emails = $namespace.Folders.Item(“Test”).Folders.Item(“Inbox”).Items | Select-Object -Property Categories,Subject,CreationTime,ReceivedTime,@{name=”Attachments”;expression={$_.Attachments|%{$_.DisplayName}}} | Sort-Object ReceivedTime -Descending

      $saveFilePath = “C:\TEMP\”

      foreach ($email in $emails)

      {

      if (($email.attachments.count -ge 1) -and (($email.Subject -match “image”) -or ($email.Subject -match “plateb”)))

      {

      foreach ($attachment in $email.attachments)

      {

      $attachment.saveasfile((join-path $savefilepath $attachment))

      }

      }

      }

      Thank you

    • #214968
      Participant
      Topics: 12
      Replies: 524
      Points: 1,220
      Helping Hand
      Rank: Community Hero
      $attachment | Out-File (join-path $savefilepath $attachment)
      
    • #215070
      Participant
      Topics: 3
      Replies: 21
      Points: 67
      Rank: Member

      Thank you very much.

    • #221367
      Participant
      Topics: 3
      Replies: 21
      Points: 67
      Rank: Member

      Helllo,

      I found an error, only the file name is saved to the file contents and the attachment itself is not saved. Can I help me?

Viewing 3 reply threads
  • You must be logged in to reply to this topic.