How to append to a file in Powershell?

Powershell offers different methods to append content to a pre-existing file. We will look at simpler ways by which you can append text to an existing file.

For our Powershell tutorial, we will look at three ways by which you can append text to a file below. We discuss the following methods in this Powershell tutorial:

  • Append text using out-file
  • Append text using redirects >>
  • Append text using Add-content

We also see the -nonewline switch and the switches to add formatted data in a file.

Append text to a file using Out-file method in Powershell

Out-file remains our favorite method to append text to a file in Powershell. For our example, we will create a text file and append a line of text to this file. This is the file to which we will append text using the out-file cmdlet in Powershell.

The following command adds the line of text to the test file that we created in the step above.

‘This is the second line. We have added this to the file using the Out-file method’ | Out-File -FilePath D:\test_folder\test2.txt -Append

  • You can see the line of static text that will be added
  • The Out-file command contains the path of the file that will get the text appended at the end of the file.

You can output the content of the file on the display screen using the Get-content cmdlet. The Get-content cmdlet is shared below for your ready reference:

Get-content -Path D:\test_folder\test2.txt

The output of these two commands is presented below in the form of a screenshot:

Append text using out-file method in Powershell

The actual file looks like the screenshot below.

Append text to a file using Redirects >>

A simple approach to appending text at the bottom of the file is through the redirect option. We will add another line of text to the test file using the redirect approach. The exact command to append text at the end of the file using the redirects is given below:

‘This is the third line that is added using the redirect symbol’ >> D:\test_folder\test2.txt

We can check the content of the file using the get-content cmdlet as given below.

Get-content -Path D:\test_folder\test2.txt

The result of these two commands is represented below in the screenshot of the text file that was appended with the text line.

Append text to the file using redirects >> in Powershell

Once we have implemented the changes, the test file looks like the screenshot below.

Both methods, using the out-file or the redirects have allowed us to append lines of text at the bottom of the file.

Append text to a file using Add-content cmdlet in Powershell

Add-content is cmdlet that can be used to add text to a file in Powershell The syntax and format of the Add-content is simple and is shared below. We will add another line to the test file and we will make use of the following command in Powershell.

Add-content d:\test_folder\test2.txt “This is the fourth line added using the Add-content cmdlet in Powershell.”

We can validate the content of the text file through the Get-content cmdlet as given below.

Get-content -Path D:\test_folder\test2.txt

The output of both commands is shared below in the screenshot.

Add-content cmdlet to a file in Powershell.

By default, new text is appended to a new line. If you were to append text to the existing line of text, you can use the -nonewline switch with the command. For our example, we have used the following Add-content cmdlet with -nonewline switch:

Add-content d:\test_folder\test2.txt -nonewline “This text gets appended to fourth line without a new line/”

We used the get-content cmdlet to display the test file contents. The screenshot below shows the test file in use.

On a similar basis, if you were to add formatted text with tab spacing, you can use the `t switch. We display both options below in the command syntax and the command output. You need to note the ` special character to insert a tab or new line character in the Add-content cmdlet.

After all these changes, the test file looks like the screenshot below.

Summary

In this Powershell tutorial, we have seen different options to append text to a file in Powershell. We have also see how we could force Powershell to append text to the same line using the -nonewline switch. On a similar basis, you can add formatted text with tab spacing using the `t switch in the cmdlet in Powershell.

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Rajesh Dhawan

Rajesh Dhawan is a technology professional who loves to blog about smart wearables, Cloud computing and Microsoft technologies. He loves to break complex problems into manageable chunks of meaningful information.