How to Write Multiple Lines to a File in PowerShell?

One common task that PowerShell administrators and developers often need to perform is writing multiple lines of text to a file. This can be for various purposes, such as logging, configuration, or documentation. In this article, we will explore different methods to write multiline strings to a file using PowerShell.

To write multiple lines to a file in PowerShell, you can use the Out-File cmdlet with a here-string for straightforward writing, or Add-Content to append text without overwriting. For more control, use Set-Content, which is faster and bypasses PowerShell’s formatting system. The redirection operator > is another quick option for writing, and >> for appending.

Write Multiple Lines to a File in PowerShell

Here, I will show you 5 methods for writing multiple lines to a file in PowerShell. We will discuss each method individually.

Method 1: Using Out-File and the Here-String

One of the easiest and simplest ways to write multiple lines to a file in PowerShell is by using the Out-File cmdlet in combination with a here-string. A here-string is a string that can span multiple lines and is declared with @” at the beginning and “@ at the end.

Here is an example of how to use a here-string with Out-File to write multiple lines to a file in PowerShell.

$multiLineText = @"
This is line one
This is line two
This is line three
"@

$multiLineText | Out-File -FilePath C:\MyFolder\file.txt

The Out-File cmdlet sends output to a file. It implicitly uses PowerShell’s formatting system to write to the file.

You can see the output in the screenshot below after I executed the PowerShell script using VS code.

Write Multiple Lines to a File in PowerShell

Method 2: Using Add-Content

If you need to append multiple lines to an existing file without overwriting the current content, you can use the Add-Content PowerShell cmdlet.

Here’s an example and the complete PowerShell script.

$lines = @"
Appending this line to the file.
Adding another line.
"@

Add-Content -Path C:\MyFolder\file.txt -Value $lines

This method is particularly useful when you need to add to a log file or update configuration files without losing existing data.

You can see the output in the screenshot below:

How to Write Multiple Lines to a File in PowerShell

Method 3: Using the Redirection Operator

PowerShell also allows you to redirect output directly to a file using the redirection operator >. In PowerShell, to append content, you’d use >> operator.

Here’s an example using the PowerShell redirection operator:

@"
This is a new line for the file.
Here's another new line.
"@ > C:\MyFolder\file.txt

And to append data:

@"
This line will be appended.
So will this one.
"@ >> C:\MyFolder\file.txt

The redirection operator is quick and easy but offers less control compared to Out-File and Add-Content in PowerShell.

Method 4: Using Set-Content

In PowerShell, you can also use the Set-Content cmdlet to write multiple lines to a file. This cmdlet is similar to Out-File, but it does not use the PowerShell formatting system and is often faster.

Here’s how you can use the PowerShell Set-Content:

$linesToWrite = @"
Line one
Line two
Line three
"@

Set-Content -Path C:\MyFolder\file.txt -Value $linesToWrite

Set-Content is a good choice when performance is a concern and when you don’t need the formatting capabilities of Out-File.

Method 5: Using a Script Block with Foreach-Object

If you have an array of strings in PowerShell that you want to write to a file line by line, you can use a script block with the Foreach-Object cmdlet.

Example:

$linesArray = "Line one", "Line two", "Line three"

$linesArray | Foreach-Object { $_ | Out-File -FilePath C:\path\to\your\file.txt -Append }

This method is useful when you have a collection of strings that you want to process individually or conditionally before writing to the file in PowerShell.

Conclusion

In this article, we’ve covered several methods to write multiple lines to a file in PowerShell. Whether you’re appending to an existing file, creating a new one, or writing line by line, PowerShell provides various cmdlets to write multiple lines or strings to a file.

The easiest way to write multiple lines to a file in PowerShell is by using the Out-File and the Here-String cmdlets.

You may also like: