‘Foreach Recipient in Recipients’ not work correctly in Powershell

Welcome Forums General PowerShell Q&A ‘Foreach Recipient in Recipients’ not work correctly in Powershell

  • This topic has 2 replies, 3 voices, and was last updated 1 month ago by
    Participant
    .
Viewing 2 reply threads
  • Author
    Posts
    • #222888
      Participant
      Topics: 3
      Replies: 1
      Points: 15
      Rank: Member

      The recipients of a mail are as follow.

      Screenshot

      I tried to use foreach to delete all of the existing recipients but it failed to delete all(but deleted some). Also the dummy Write-Host worked in similar way.

      But if test it only with dummy Write-Host, its output times are exactly same as the number of recipients.

      See below:

      [DBG]: PS C:\Users\carrot\Desktop\Quotation\AutoSendTender>> foreach ($objRecipient in $objMailItem.Recipients){Write-Host $objRecipient.Name}
      aaaa@yandex.com
      cccc@yandex.com
      bbbb@yandex.com
      dddd@yandex.com
      
      [DBG]: PS C:\Users\carrot\Desktop\Quotation\AutoSendTender>> foreach ($objRecipient in $objMailItem.Recipients){Write-Host $objRecipient.Name;$objRecipient.Delete()}
      aaaa@yandex.com
      bbbb@yandex.com

      Why and how to fix? Thanks.

    • #222903
      Participant
      Topics: 12
      Replies: 1623
      Points: 2,565
      Helping Hand
      Rank: Community Hero

      If you are using Outlook, the method is Remove, not Delete.

      https://docs.microsoft.com/en-us/office/vba/api/outlook.recipients.remove

    • #222909
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      Richard, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

      When you crosspost the same question at the same time to different forums you should at least post links to the other forums along with your question to avoid people willing to you help making their work twice or more.

      https://stackoverflow.com/questions/61461458/foreach-recipient-in-recipients-not-work-correctly-in-powershell

      Thanks

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