Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9140869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:34:22+00:00 2026-06-17T09:34:22+00:00

I have a powershell script that pulls emails using EWS managed API, reads their

  • 0

I have a powershell script that pulls emails using EWS managed API, reads their subject, from address, to address, and internetmessageheaders. Within the email read loop it loads all this information into a datatable.

I need to grab all the above information for each message and populate an array or datatable based on the from address. Then I’ll check for relay information and send the whole list based on from address to a target address.

I can’t seem to wrap my head around how to build the from address lists. I’ve tried turning the datatable data into a hashtable and then using a sort|get-unique to get a list of the unique from addresses but have not been able to use this information to correctly build the from address array. I’ve tried looping through using a select-string $_ -allmatches but haven’t gotten anywhere.

Here is part of my code, It creates the datatable and populates it using information from each email.

$msgTable = New-Object system.Data.DataTable “Messages”
$col1 = New-Object system.Data.DataColumn Subject,([string])
$col2 = New-Object system.Data.DataColumn From,([string])
$col3 = New-Object system.Data.DataColumn To,([string])
$col4 = New-Object system.Data.DataColumn Relay,([string])
$msgTable.columns.add($col1)
$msgTable.columns.add($col2)
$msgTable.columns.add($col3)
$msgTable.columns.add($col4)

$frItemResult = $PublicFolder.FindItems($sfCollection,$view)
# Loop through view results and mark read
$frItemResult | ForEach-object{
if($_.HasAttachments ) {
$_.Load()
"Report Subject: $($_.Subject)"
# Loop through attachments, extract info
$_.Attachments | ForEach-object  {
if($_.Name -notmatch "ATT00001"){
$_.Load($attPropset)

$row = $msgTable.NewRow();$row.Subject = $($_.item.Subject); $row.From = ($_.item.From.address); $row.To = $($_.item.ToRecipients.address); $row.Relay = "$($_.Item.InternetMessageHeaders | Where-Object {$_.name -like "*received*"})";$msgTable.Rows.Add($row) 

Here is what $msgTable looks like after data is populated.

Subject                          From                             To                               Relay                           
-------                          ----                             --                               -----                           
Message1                        user1@company.com           recipient1@yahoo.com               Received-SPF=pass (domain of ...
Message2                        user2@company.com           recipient2@comcast.net             Received=from imta27.mailguys...
Message2                        user2@company.com           recipient3@yahoo.com               Received-SPF=pass (domain of ...
Message3                        user3@company.com           recipient4@sbcglobal.net           Received-SPF=pass (domain of ...

I need to be able to grep out all the information for user2@company.com to another variable and do the same for any number of other repeating or non-repeating from addresses.

I’ll then take each of the from address variables and send it to the target recipient.

Any help is greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:34:22+00:00Added an answer on June 17, 2026 at 9:34 am

    Try using Group-Object, you can then run a foreach loop against each grouping:

    $Grouping = $msgTable | Group-Object -Property From
    foreach ($Group in $Grouping)
    {
        Write-Host $Group.Name -ForegroundColor "Green"
        $Group.Group | ft -Auto
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Powershell script that reads values off of the pipeline: PARAM (
I have a powershell script that needs to read from a config file with
I have a powershell script that modifies transport rules. It works perfectly from powershell
I have a powershell script that works wonderfully from the powershell shell and from
I have a PowerShell script that uses du.exe ( Disk Usage originally from Sysinternals)
Does anyone have a handy powershell script that gets a set of files from
I have a Powershell script that copies files from one location to another. Once
I have a powershell script that works fine if I run it from an
I have a powershell script that will generate an email for users whose password
I have a complex Powershell script that gets run as part of a SQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.