How to break lines in PowerShell?

user454322 picture user454322 · Aug 9, 2012 · Viewed 205.6k times · Source

I am [completely new to PowerShell and] concatenating a string in a loop, if a special condition occurs I should insert a line break...how can I do this?

Basically looking for the equivalent of \n.

$str = ""
foreach($line in $file){
  if($line -Match $review){ #Special condition
    $str += ANSWER #looking for ANSWER
  }
  #code.....
}

So far I have tried

"\n" '\n' "\N" '\N' "\r" '\r' "\R" '\R' '`n' '`r' '-n' '-r' 

Answer

Neverever picture Neverever · Aug 9, 2012

Try "`n" with double quotes. (not single quotes '`n' )

For a complete list of escaping characters see:

Help about_Escape_character

The code should be

$str += "`n"