How do I remove all newlines from a string in PowerShell?
Asked Answered
S

2

19

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.

Standridge answered 29/1, 2013 at 21:34 Comment(5)
for html message, new line will be <br/> no ?Convenient
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 :)Weber
You could also try PowerShell's split operator e.g. $message.Body -split "'r?'n" (substitute backtick for ').Ferren
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.Standridge
@Graimer forgot to reference you in my previous commentStandridge
A
41

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",", ")
Anderaanderea answered 5/2, 2013 at 5:5 Comment(2)
Wouldn't it be best to use learn.microsoft.com/en-us/dotnet/api/…?Hibernia
Generally speaking, sure, prefer [System.Environment]::NewLine but where [system.Environment]::NewLine -eq "`r`n" and text is sprinkled with `r or `n alone... YMMV?Anderaanderea
K
19

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

$foo = [string]::join("",($foo.Split("`n")))
Kerosene answered 12/7, 2020 at 21:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.