18

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
EGr
  • 2,072
  • 10
  • 41
  • 61
  • for html message, new line will be
    no ?
    – Loïc MICHEL Jan 29 '13 at 22:05
  • 4
    Have you tried using (...Replace("'r'n",", ")) (replace ' with backtick) ? If the Replace method doesn't work, try`$($message.Body -join ", ")` instead. That's the way to do it if it's actually a string-array `string[]`. 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 :) – Frode F. Jan 29 '13 at 22:18
  • You could also try PowerShell's split operator e.g. `$message.Body -split "'r?'n"` (substitute backtick for '). – Keith Hill Jan 29 '13 at 23:37
  • I've attempted all of these individually, and together, and nothing seems to work. It may have something to do with the add-content cmdlet. When I write the file as a CSV, the file is only one line (but imports as multiple lines when imported into Excel); but if I write it as a txt, it has the line breaks in notepad as well... So the problem still isn't solved, but I might be getting closer. – EGr Jan 30 '13 at 15:38
  • @Graimer forgot to reference you in my previous comment – EGr Jan 30 '13 at 15:50

2 Answers2

38

Commenters point to the return `r and maybe that special character should be replaced separately. Guessing this could be done with the .replace() method and some regex. Or more simply (and clumsily I admit) with another variable before your $list1 += line, such as:

$bod = $message.body -replace "`n",", " -replace "`r",", "

Overkill, but here's a small example from scratch. I'm suggesting you add $y to make it easier to manipulate the message body.

$x = new-object -type psObject
$x | add-member memburr1 -membertype noteproperty -value "no trouble here"
$x | add-member memburr2 -membertype noteproperty -value "uh `n oh `r spaghettio"

$y = $x.memburr2 -replace "`n",", " -replace "`r",", "

$z = @()
$z += "$($x.memburr1);$y"

If this doesn't help, I'd be curious what appears immediately before and after a problematic line break in the output.

EDIT: Or use the .replace() method twice:

$x.memburr2.replace("`n",", ").replace("`r",", ")
noam
  • 1,914
  • 2
  • 20
  • 26
15

None of the above worked for me. What did work was:

$foo = [string]::join("",($foo.Split("`n")))
Jason DeMorrow
  • 489
  • 5
  • 9