Extract Text From Email

Hi All

I’m new to the scene of PS & have been thrown in the deep end, from my perspective, as I don’t done PS at all & just beginning out with it.

I require help. I need to write a script that will go off to get the latest email from a specific account, then output the content to a csv, or assigned to variables.

 

The email content will have the following;

Email Example:

First Name: First

Last Name: Last

Location: Where

Role: What

Manager: Who

 

Require Output:

First,Last,Where,What,Who

 

I would look at the ConvertFrom-String command. It’s able to take a template you create and parse text based on that template.

You don’t say where you need to do this from.

Desktop Outlook - you’d have to use the Outlook DOM (Document Object Model) with PowerShell and DCOM
Enterprise Exchange - you’d have to leverage the EWS API.

Postanote is correct. If you’re talking about Exchange/Outlook, a simple command will not do the job. Those will only get you topical information about a message (sender, recipient, message subject, etc). This requires a little fancy footwork using Exchange EWS or Outlook Interop Assemblies that can dig deep into an email.

Check out this article by Ed Wilson to give you an idea: https://blogs.technet.microsoft.com/heyscriptingguy/2011/05/26/use-powershell-to-data-mine-your-outlook-inbox/

Hi All

After some digging around. I found bits & pieces that i that would resolve my issue.

 

Please forgive my coding. I’ve currently got it to output to the host, just to check if it works. To which after some testing, i can say this works for me.

#Add-Type -Assembly “Microsoft.Office.Interop.Outlook”
#$Outlook = New-Object -ComObject ‘Outlook.Application’
#$namespace = $Outlook.GetNameSpace(“MAPI”)
#$inbox = $namespace.GetDefaultFolder(

[Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)

$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace(“MAPI”)
$folder=$namespace.GetDefaultFolder(6)
$folder.Items |
#Select Email with matching subject line
?{$.subject -match “New User Request” } |
sort receivedtime -desc |
%{
echo $
.body #do stuff with body

Sets what to collect from the Email

$RE = [RegEx]’(?sm)FirstName:\s+(?<FirstName>.?)$.?LastName:\s+(?<LastName>.?)$.?Location\s*:\s*(?<Location>.?)$.Role:\s+(?<Role>.?)$.?Manager:\s+(?<Manager>.?)$.?’
#Collects User From Email
ForEach ($item in $inbox.items){

if (($item.body -match $RE )){
[PSCustomObject]@{
FirstName = $Matches.FirstName
LastName = $Matches.LastName
Location = $Matches.Location
Role = $Matches.Role
Manager = $Matches.Manager
}
}

}
#mark as read
$_.Unread=$false
}
Write-Output $Matches.FirstName
Write-Output $Matches.LastName
Write-Output $Matches.Location
Write-Output $Matches.Role
Write-Output $Matches.Manager

Yes, this will work.

And you don’t need to Write-Output, since PSCustomObject will produce an output for you.

Thank you.

So my next task is to log on to a specific email but still using the code above.

what would i need to add to get this result

the write-output was for me.