How to Write to File without Carriage Return in PowerShell

Recently, I was working on a PowerShell script that needed to output data to a file in a very specific format. The challenge was that I needed to write content to a file without adding carriage returns (line breaks) at the end of each line.

This can be particularly important when you’re generating configuration files, creating CSV data with custom formatting, or preparing text for specific applications that require precise formatting.

In this article, I’ll show you several methods to write to a file without carriage return in PowerShell. I’ve been using these techniques for years, and they’ve saved me countless hours of troubleshooting formatting issues. So let’s dive in!

Method 1 – Using Out-File with NoNewline Parameter

The simplest approach to write content to a file without adding a carriage return is to use the Out-File cmdlet with its -NoNewline parameter. This parameter tells PowerShell not to add a newline character at the end of the output.

Here’s how to use it:

"This is some text without a carriage return at the end" | Out-File -FilePath C:\Bijay\output.txt -NoNewline

If you need to append more text to the same file without adding line breaks:

"More text added" | Out-File -FilePath C:\Bijay\output.txt -Append -NoNewline

The result will be a single line containing: “This is some text without a carriage return at the endMore text added”

I executed the above cmdlets, and you can see the exact output in the screenshot below:

"More text added" | Out-File -FilePath C:\Bijay\output.txt -Append -NoNewline

Check out Append Text to Files in PowerShell

Method 2 – Using Add-Content with NoNewline Parameter

Similar to Out-File, the Add-Content cmdlet also supports a -NoNewline parameter. This is particularly useful when you want to append content to an existing file:

Add-Content -Path C:\Bijay\output.txt -Value "First piece of text" -NoNewline
Add-Content -Path C:\Bijay\output.txt -Value "Second piece of text" -NoNewline

The benefit of Add-Content is that it’s designed for appending content, making it slightly more efficient than Out-File -Append in some scenarios.

Method 3 – Using .NET StreamWriter Class

For more control over file operations, you can use the .NET StreamWriter class. This approach gives you granular control over how text is written to files:

$filePath = "C:\temp\output.txt"
$streamWriter = New-Object System.IO.StreamWriter $filePath
$streamWriter.Write("First part")
$streamWriter.Write("Second part")
$streamWriter.Close()

The key here is using the Write() method instead of WriteLine(), which would add a newline character. Don’t forget to close the StreamWriter when you’re done to ensure all content is properly written to the file.

Check out PowerShell Write to File

Method 4 – Using the -join Operator

Sometimes, you might have an array of items that you want to write to a file as a single line. The -join operator lets you combine array elements without newlines:

$items = @("Item1", "Item2", "Item3")
$items -join "" | Out-File -FilePath C:\Bijay\output.txt -NoNewline

You can also specify a delimiter if needed:

$items = @("Item1", "Item2", "Item3")
$items -join "," | Out-File -FilePath C:\Bijay\output.txt -NoNewline

This would output: “Item1,Item2,Item3” without a trailing newline.

You can see the exact output in the screenshot below:

powershell write to file without carriage return

Check out Rename Multiple Files Using PowerShell

Method 5 – Replacing Carriage Returns in Existing Content

Sometimes you might need to replace carriage returns in strings that already contain them. PowerShell makes this straightforward:

$textWithReturns = "Line1`r`nLine2`r`nLine3"
$textWithoutReturns = $textWithReturns -replace "`r`n", ""
$textWithoutReturns | Out-File -FilePath C:\temp\output.txt -NoNewline

This technique is especially useful when working with text from external sources where you don’t control the original formatting.

Practical Examples and Use Cases

Now let me show you some practical examples and user cases from my work experience.

Example 1: Creating a CSV file with custom formatting

When generating CSV files, you might need precise control over line endings. You can use the below PowerShell script.

$filePath = "C:\Bijay\custom.csv"
"Name,Age,Location" | Out-File -FilePath $filePath -NoNewline
$users = @(
    [PSCustomObject]@{ Name = "John"; Age = 30; Location = "New York" }
    [PSCustomObject]@{ Name = "Sarah"; Age = 25; Location = "Chicago" }
)

foreach ($user in $users) {
    "`r`n$($user.Name),$($user.Age),$($user.Location)" | Out-File -FilePath $filePath -Append -NoNewline
}

Example 2: Building a configuration file

When building configuration files for applications, precise formatting is often required; you can see the complete PowerShell script.

$configPath = "C:\Bijay\app.config"
"[Settings]" | Out-File -FilePath $configPath -NoNewline
"`r`nServer=" | Out-File -FilePath $configPath -Append -NoNewline
"localhost" | Out-File -FilePath $configPath -Append -NoNewline
"`r`nPort=" | Out-File -FilePath $configPath -Append -NoNewline
"8080" | Out-File -FilePath $configPath -Append -NoNewline

Example 3: Creating a log parser

When parsing logs, you might need to create custom output formats:

$logEntries = Get-Content C:\logs\system.log
$outputPath = "C:\temp\parsed_log.txt"

foreach ($entry in $logEntries) {
    if ($entry -match "ERROR") {
        $timestamp = [regex]::Match($entry, '\d{2}:\d{2}:\d{2}').Value
        $message = [regex]::Match($entry, 'ERROR: (.+)').Groups[1].Value

        "[$timestamp]" | Out-File -FilePath $outputPath -Append -NoNewline
        " $message | " | Out-File -FilePath $outputPath -Append -NoNewline
    }
}

Tips and Best Practices

  1. Test your output: Always verify the output is exactly what you expect, especially when working with complex formats.
  2. Be aware of encoding: If you’re dealing with special characters, specify the encoding using the -Encoding parameter:
   "Special characters: éçñ" | Out-File -FilePath C:\temp\output.txt -NoNewline -Encoding UTF8
  1. Use global variables carefully: When working across scripts, consider using PowerShell global variables to maintain consistent file paths and configurations.
  2. Error handling: Implement try-catch blocks for file operations to handle potential issues:
   try {
       "Some content" | Out-File -FilePath C:\temp\output.txt -NoNewline
   } catch {
       Write-Error "Failed to write to file: $_"
   }
  1. Clean up resources: When using .NET classes like StreamWriter, always close or dispose of them properly to avoid file locking issues.

Conclusion

In this tutorial, I have explained how to write to file without carriage return in PowerShell using five different methods. This will help you creating exactly the output format you need while building configuration files, processing data, or generating reports, etc.

The Out-File and Add-Content cmdlets with -NoNewline offer simplicity, while the StreamWriter approach provides more granular control for complex operations.

I hope you found this article helpful. If you have any questions or other techniques you use to handle carriage returns in PowerShell, please share them in the comments below!

You may also like:

Power Apps functions free pdf

30 Power Apps Functions

This free guide walks you through the 30 most-used Power Apps functions with real business examples, exact syntax, and results you can see.

Live Webinar

Quiz App Using SharePoint Framework (SPFx)

Learn to built a complete Quiz Management solution that enables admins to create and manage quizzes, categories, questions, and settings with an easy automated setup process in SharePoint. It also includes an interactive quiz experience for users and a powerful dashboard to track participation, analyze results, and view detailed performance reports with charts and answer insights.

📅 2nd June 2026 – 10:00 AM EST | 7:30 PM IST

Download User registration canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App