This is the code I am using to parse my emails. If they match a specific date, I want to add them to a list of other emails, then make that into a flat file:
outfile = "C:\Temp\emails.csv"
$olFolderInbox = 6
$ol = new-object -comobject "Outlook.Application"
$mapi = $ol.getnamespace("mapi")
$inbox = $mapi.GetDefaultFolder($olFolderInbox)
$msgs = $inbox.Folders.Item("root")
$list1 = @()
foreach($message in ($msgs.items))
{
if($message.ReceivedTime -gt $(get-date).adddays(-14))
{
$list1 += "$($message.Subject);$($message.ReceivedTime);$($message.Body.Replace("`n",", "))"
}
}
if(Test-Path $outfile)
{
Remove-Item $outfile
Add-Content $outfile $list1
}
else
{
Add-Content $outfile $list1
}
The problem I run into is that the replace statement on $message.Body.Replace("`n",", ")
doesn't actually remove newlines, and the file doesn't get created appropriately. Is there a way to confirm that the entire contents of the body portion become a single line?
I have confirmed that the $message.body
object is a string, so I'm not certain why this is not working.
$($message.Body -join ", ")
instead. That's the way to do it if it's actually a string-arraystring[]
. I don't have outlook so I can check the object type myself, so putting this as a comment first :-) Kayasax's is onto something too. You got 3 things to try now :) – Weber$message.Body -split "'r?'n"
(substitute backtick for '). – Ferren