none
Get Email Addresses using Power shell from Email RRS feed

  • Question

  • Hi,

    I am able to read the email body content through variable $a as below via Power shell

    At last I want to fetch email addresses in a separate variable.

    Please share your inputs to achieve this.

    Test

    Sample

    Sender: sender@sender .net

    Subject: Subject

    Affected Users:

    Email Address: abc@ abc.com

    Email Address: def@ def.com

    Email Address: xyz@xyz.com

    Email Address: iis@iis.com

    Respectfully,

    Test

    Sample

    Regards,

    Sudheer


    Thanks & Regards, Sudheer

    Thursday, May 9, 2019 10:22 AM

Answers

  • Hi,

    Based on the script Vasil L. Michev provided, we made some modification:

    $a=" Test Sample Sender: sender@sender .net Subject: Subject Affected Users: Email Address: abc@ abc.com Email Address: def@ def.com Email Address: xyz@xyz.com Email Address: iis@iis.com Respectfully, Test Sample " $aa=$a | Select-String "\b([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})\b" -AllMatches | ForEach-Object { $_.Matches.Value } for ($i=0; $i -le $aa.Length-1 ;$i++) { Write-Output "a$i = $($aa[$i])" } $a_0 $a_1

    Here is the output:

    Regards,

    Lydia Zhou


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.

    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Thursday, May 16, 2019 8:14 AM
    Moderator

All replies

  • Might help if you tell us exactly how you are creating the variable...
    Thursday, May 9, 2019 7:04 PM
  • Hi,

    If type $a in power shell I am able to see the value as below

    Now I need to extract email addresses under Affected users and put in separate variables

    Test

    Sample

    Sender: sender@sender .net

    Subject: Subject

    Affected Users:

    Email Address: abc@ abc.com

    Email Address: def@ def.com

    Email Address: xyz@xyz.com

    Email Address: iis@iis.com

    Respectfully,

    Test

    Sample

    Regards,

    Sudheer


    Thanks & Regards, Sudheer

    Friday, May 10, 2019 4:31 AM
  • And how did you create $a??? Is it a string variable, is it a list, a proper object from which we can reference properties?
    Friday, May 10, 2019 6:24 AM
  • Hi,

    After getting the email content i just assigned it to variable as below

    So $a can be considered as a string variable

    $a="

    Test

    Sample

    Sender: sender@sender .net

    Subject: Subject

    Affected Users:

    Email Address: abc@ abc.com

    Email Address: def@ def.com

    Email Address: xyz@xyz.com

    Email Address: iis@iis.com

    Respectfully,

    Test

    Sample

    "


    Thanks & Regards, Sudheer

    Friday, May 10, 2019 6:27 AM
  • Hi,

    Do you mean you want to check the message content, and get all email addresses mentioned in the email content? If we misunderstood, please point out.

    Or you just want to get the senders, subject and recipients of the emails in your organization?

    Based on my knowledge, no existing Exchange commands support to check messages, and help to filter the message content to output some specified information.

    Regards,

    Lydia Zhou


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.

    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Friday, May 10, 2019 8:58 AM
    Moderator
  • Hi,

    I am able to achieve a bit as below

    $a=$a.Substring($a.IndexOf("Affected Users:")+1,($a.IndexOf("Respectfully")-1)-($a.IndexOf("Affected Users:")+1))

    Now $a has values as below

    ffected Users:

    Email Address: abc@ abc.com

    Email Address: def@ def.com

    Email Address: xyz@xyz.com

    Email Address: iis@iis.com

    Now at last I need to get each and every email address in the variable

    Regards,

    Sudheer


    Thanks & Regards, Sudheer

    Friday, May 10, 2019 1:28 PM
  • For a string variable, your only real option is to use regex:

    $a | Select-String "\b([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})\b" -AllMatches | ForEach-Object { $_.Matches.Value }
    
    abc@abc.com
    def@def.com
    xyz@xyz.com
    iis@iis.com

    Friday, May 10, 2019 5:44 PM
  • Hi,

    Thanks for the update.

    At last i need the each email address as below i.e.

    a1=abc@abc.com

    a2=def@def.com

    a3=xyz@xyz.com

    a4=iis@iis.com

    Regards,

    Sudheer


    Thanks & Regards, Sudheer

    Tuesday, May 14, 2019 11:22 AM
  • Hi,

    Based on the script Vasil L. Michev provided, we made some modification:

    $a=" Test Sample Sender: sender@sender .net Subject: Subject Affected Users: Email Address: abc@ abc.com Email Address: def@ def.com Email Address: xyz@xyz.com Email Address: iis@iis.com Respectfully, Test Sample " $aa=$a | Select-String "\b([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})\b" -AllMatches | ForEach-Object { $_.Matches.Value } for ($i=0; $i -le $aa.Length-1 ;$i++) { Write-Output "a$i = $($aa[$i])" } $a_0 $a_1

    Here is the output:

    Regards,

    Lydia Zhou


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.

    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Thursday, May 16, 2019 8:14 AM
    Moderator