title: How do I remove carriage returns from text file using Powershell?
body: <p>I'm outputting the contents of a directory to a txt file using the following command:</p>
  
  <pre><code>$SearchPath="c:\searchpath"
  $Outpath="c:\outpath"
  
  Get-ChildItem "$SearchPath" -Recurse | where {!$_.psiscontainer} | Format-Wide -Column 1'
  | Out-File "$OutPath\Contents.txt" -Encoding ASCII -Width 200
  </code></pre>
  
  <p>What I end up with when I do this is a txt file with the information I need, but it adds numerous carriage returns I don't need, making the output harder to read.</p>
  
  <p>This is what it looks like:</p>
  
  <pre><code>    c:\searchpath\directory
  
  name of file.txt
  
  name of another file.txt
  
  
      c:\searchpath\another directory
  
  name of some file.txt
  </code></pre>
  
  <p>That makes a txt file that requires a lot of scrolling, but the actual information isn't that much, usually a lot less than a hundred lines.</p>
  
  <p>I would like for it to look like:</p>
  
  <pre><code>  c:\searchpath\directory
  nameoffile.txt
    c:\searchpath\another directory
  another file.txt
  </code></pre>
  
  <p>This is what I've tried so far, not working</p>
  
  <pre><code>$configFiles=get-childitem "c:\outpath\*.txt" -rec
  foreach ($file in $configFiles)
  {
  (Get-Content $file.PSPath) | 
  Foreach-Object {$_ -replace "'n", ""} | 
  Set-Content $file.PSPath
  }
  </code></pre>
  
  <p>I've also tried 'r but both options leave the file unchanged.</p>
  
  <p>Another attempt:</p>
  
  <pre><code>Select-String -Pattern "\w" -Path 'c:\outpath\contents.txt' | foreach {$_.line}'
  | Set-Content -Path c:\outpath\contents2.txt
  </code></pre>
  
  <p>When I run that string without the Set-content at the end, it appears exactly as I need it in the ISE, but as soon as I add the Set-Content at the end, it once agains carriage returns where I don't need them. </p>
  
  <p>Here's something interesting, if I create a text file with a few carriage returns and a few tabs, then if I use the same -replace script I've been using, but use<code>t to replace the tabs, it works perfect. But</code>r and <code>n do not work. It's almost as though it doesn't recognize them as escape characters. But if I add</code>r and `n in the txt file then run the script, it still doesn't replace anything. Doesn't seem to know what to do with it.</p>