Append Data to File using PowerShell

Append data to file using PowerShell

Appending Text to a Text File

We have various approaches to append content in a text file using PowerShell. For example, we can append information on the same line, on a new line with/without special characters, on multiple lines, and append formatted data such as tabular content. But, before moving towards possible solutions, it is mandatory to know about the file and file content we will use for every approach below in this section. The file.txt lives in the E:\Test directory.

Note that our cursor is on the same line, right after the period (.), which will help us to append on the same line. Let’s use this file to explore the various solutions below.

Use Out-File Cmdlet

Use the Out-File cmdlet with the -Append parameter to append content on the same line in the file.txt file.

Use the Out-File cmdlet with the -Append parameter to append content on the new line in the file.txt file.

Did you find any similarity in the above two examples? Yes! Why is it so? Because -Append was used the first time in the first example, it appended the content on the same line as our cursor was on the same line but added a new line character at the end of the file as well. Now, the cursor is on a new line; this is why -Append in the second code example added the content on a new line, but it also added a new line character at the end of the file. Note that the -Append parameter automatically adds a new line character at the end of the file; that’s why the content is appended on the new line every time except the first example.

Use the Out-File cmdlet with the -Append parameter to append content with special characters (such as tab) in the file.txt file.

Use the Out-File cmdlet with the -Append parameter to append multiple lines in the file.txt file.

Use the Out-File cmdlet with the -Append parameter to append tabular content (formatted content) in the file.txt file.

Use the Out-File cmdlet to append a line having single and double quotes in the file.txt file .

The Out-File cmdlet is used to send output to a specified file. This cmdlet implicitly uses the formatting system of PowerShell to write to a file. Note that this file will get the exact display representation as we had on the terminal, which means the output may only be good enough for programmatic processing if every input object involved in the output is a string. We used this cmdlet to send output ($content) to the file.txt file.

In the above examples, we created two variables named $filePath and $content. The $filePath contained the path of the file.txt file, while $content had the actual content we wanted to append. The Get-Content cmdlet was used to retrieve the entire data (content) of the file.txt file and display it on the terminal, which was helpful to see the updated content without opening the file.txt file.

The only difference was how we used the Out-File cmdlet based on the requirements. The -FilePath, -InputObject, and -Append parameters were the same in all examples. So what do these parameters mean, and why did we use them?

The -FilePath was used to specify $filePath, -InputObject was used to mention the $content that we wanted to append and -Append was used to append $content at the end of the file.txt file. You can also use the -Encoding parameter to specify the encoding type for a target file. Usually, UTF8 encoding is used while utf8NoBOMis its default value. We have other encodings as well that we can use based on our requirements.

Now, the point is how the content is formatted. It is done while initializing each example’s $content variable. We set the $content variable in the first and second code snippets with a simple text. In the third example, we initialized the $content with a text preceded by a `t (special character) to append the text after a tab.

Similarly, in the fourth example, we used an array operator represented by @() to create an array of strings where each string was a line of text that we appended in the file at once. Finally, in the fifth example, we used @() with special characters to append data in a tabular form; these special characters include a newline character and a tab character. You can find different special characters with their description below.

Special Character Description
`0 Null           
`a Alert          
`b Backspace      
`n Newline        
`r Carriage Return
`t Horizontal Tab 
`' Single Quote   
`" Double Quote   

In this section’s seventh and last example, we used single and double quotes while initializing the value of $content. Note that each quote (single and double) was preceded by a backtick, as demonstrated in the above table.

If the file, which was file.txt in our case, does not exist, the Out-File cmdlet will create it and perform the rest of the operations.

Use Add-Content Cmdlet

Use the Add-Content cmdlet to append content on the same line in the file.txt file.

Use the Add-Content cmdlet to append content on the new line in the file.txt file.

Use the Add-Content cmdlet to append content with special characters (such as tab) in the file.txt file.

Use the Add-Content cmdlet to append multiple lines in the file.txt file.

Use the Add-Content cmdlet to append tabular content (formatted content) in the file.txt file.

Use the Add-Content cmdlet to append a line having single and double quotes in the file.txt file.

We used the Add-Content to add content to the given file. We can use this cmdlet by specifying the text in the command directly or store it in an object and use that object while using the Add-Content cmdlet. For example, we stored the content in the $content variable and used that variable while using this cmdlet; see the above code fences.

The examples of this section are similar to those using the Out-File cmdlet. Again, we defined and initialized the $filePath and $content to contain the file path and content. The Get-Content was used to get content from the file.txt file and show it on the terminal. The content stored in the $content variable was formatted exactly as we did in the previous section while using the Out-File cmdlet; you can refer to that.

In all examples for this section, we used -Path and -value parameters with the Add-Content cmdlet to specify the $filePath and $content.

The Add-Content will create the text file with a specified name if it does not exist already. If the path is not given, the text file will be created in the current directory.

Use Redirection (>>) Operator

Use the >> operator to append content on the same line in the file.txt file.

Use the >> operator to append content on the new line in the file.txt file.

Use the >> operator to append content with special characters (such as tab) in the file.txt file.

Use the>> cmdlet to append multiple lines in the file.txt file.

Use the >> operator to append tabular content (formatted content) in the file.txt file.

Use the >> operator to append a line having single and double quotes in the file.txt file.

These examples, cmdlets, and variables are the same as in previous sections, but we used the redirection operator to append the file.txt file.

The redirection operator will create the text file in the specified directory (or in the current directory if not given) if it does not already exists.

Appending One Text File to Another

Use Add-Content to append the content of one text file into another text file in PowerShell.

That’s all about how to append data to file using PowerShell.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *