How to Append Text to a File in PowerShell?

Recently, I was working with a logging application. There, I got a requirement to append text to a file in PowerShell. In this PowerShell tutorial, I will show different methods to append data to a text file using PowerShell and how to append text to a new line in a text file using PowerShell.

To append text to a file in PowerShell, the Add-Content cmdlet is commonly used due to its simplicity and direct approach. For instance, using Add-Content -Path “C:\example\log.txt” -Value “Appended text” will add “Appended text” to the end of log.txt. Alternatively, you can use the Out-File cmdlet with the -Append parameter, or the >> redirection operator for quick appends, such as “New data” >> “C:\example\log.txt”.

Append Data to a File using PowerShell

Here are a few methods to append data to a file using PowerShell with a complete script.

1. Using Add-Content Cmdlet

To append to a text file in PowerShell, use the Add-Content cmdlet. This cmdlet allows you to add text to the end of a file without overwriting the existing content.

Here is an example.

Add-Content -Path "C:\MyFolder\log.txt" -Value "This is the new line to append."

This script adds the sentence “This is the new line to append.” to the file log.txt. If log.txt does not exist, it will be created with the content provided.

You can see the output in the screenshot below after I executed the above script using VS Code.

Append Text to a File in PowerShell

2. Using Out-File Cmdlet with -Append Parameter

Another method is using the PowerShell Out-File cmdlet with the -Append parameter. This cmdlet is used for outputting data to a file, and when combined with -Append, it will add the data to the end of the file rather than overwriting it.

Example:

Get-Process | Out-File -FilePath "C:\MyFolder\processes.txt" -Append

In this example, the list of currently running processes is appended to processes.txt. Each time this script is run, the current process list is added to the file.

Check out PowerShell Copy-Item

3. Using Redirection Operators

PowerShell also supports the use of redirection operators, such as >>, which can be used to append output to a file.

Example:

"Another line to append using Redirection operator" >> "C:\MyFolder\log.txt"

This one-liner will append “Another line to append” to log.txt. It’s a quick and easy method, especially for simple text appending.

You can see the output in the screenshot below. I executed the above PowerShell script.

powershell write to file append

4. Using Set-Content with -NoClobber

The PowerShell Set-Content cmdlet can be used in conjunction with the -NoClobber parameter to ensure that you do not accidentally overwrite a file when you intend to append to it.

Example:

Set-Content -Path "C:\MyFolder\log.txt" -Value "Data to append" -NoClobber

If log.txt already exists, this command will not execute and the file will not be modified.

5. Using Advanced Appending Techniques

For more complex appending scenarios, you might want to incorporate condition checks, loops, or other logic into your script.

Example:

$file = "C:\MyFolder\log.txt"
$dataToAppend = "This data will be appended with a timestamp."

if (Test-Path $file) {
    Add-Content -Path $file -Value "$((Get-Date).ToString()): $dataToAppend"
} else {
    Write-Host "File does not exist."
}

This script checks if log.txt exists before appending. It also prefixes the data with a timestamp, providing more context to the appended information.

PowerShell write to file append new line

To append text to a new line in a text file using PowerShell, you can use the Add-Content cmdlet. Here’s an example:

# Define the file path and the text to append
$filePath = "C:\MyFolder\file.txt"
$textToAppend = "This is the new line of text."

# Append the text to the file with a new line
Add-Content -Path $filePath -Value "`n$textToAppend"

In this example, the backtick character followed by n ("n”`) is used to add a newline before appending the text. This ensures that the new text is added on a new line in the file.

After I executed the PowerShell script using VS Code, You can see the output in the screenshot below:

PowerShell write to file append new line

Conclusion

Appending data to a file in PowerShell is possible using various cmdlets and operators. In this PowerShell tutorial, I have explained how to append text to a file in PowerShell using the Add-Content Cmdlet, the Out-File Cmdlet with -Append Parameter, and others. I have also explained one example of “PowerShell write to file append new line“.

You may also like the following PowerShell tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.