Powershell

How to Append Data to a File Using PowerShell

The process of adding new data to the existing content of a file is known as Appending. Many users often utilize text files to save their data; however, it can be a waste of time to open the file, add the required data, and close the selected file, depending upon the file location and the size. Instead, you can use PowerShell commands to append data into a file within a few seconds.

In PowerShell, we use the Add-Content cmdlet to append data in a file, and this operation can be verified using the Get-Content command.

This blog will explain how to append data to a file using the Add-Content PowerShell command. Let’s get started!

How to append data using PowerShell?

In PowerShell, the Add-Content cmdlet is used to append data in a file. The content which needs to be appended is specified in this command.

Syntax
To add or append any data in a file, use the below-given syntax of the Add-Content cmdlet:

> Add-Content [-Path] <sourcefile> [-Value] <content>

Here, -Path tells the exact location of the file, and the -Value is the text which will be appended in it.

Let’s try to append data in a file using the above cmdlet.

Example 1: Append data in a new line
We have a text file named Powershell4 in the E:\Powershell tutorial\New folder:

As you can see, there is already some text in the opened file:

We will append the data in the next line of the mentioned file by using the PowerShell Add-Content cmdlet:

> Add-Content -Path "E:\Powershell tutorial\New folder\Powershell4.txt" -value "to automate tasks."

Now, execute the Get-Content command to view the content of the Powershell4 file:

> Get-Content "E:\Powershell tutorial\New folder\Powershell4.txt

The output will append content in a new line. It is also considered the default behavior of PowerShell:

You can also open up the specified file for the verification:

Example 2: Append data in the same line
In case you need to append the data in front of the existing text without shifting to the new line, then add the -nonewline option with the Add-Content PowerShell command:

> Add-Content -Path "E:\Powershell tutorial\New folder\Powershell4.txt" -value "to automate tasks." -nonewline

Then, run the Get-Content PowerShell cmdlet to print out the content of the Powershell4 text file:

> Get-Content "E:\Powershell tutorial\New folder\Powershell4.txt

Output

The given image indicates that now data is appended in the same line:

How to append data to a file using PowerShell Special Characters?

In PowerShell, there exist some other ways to append data that can be performed by using the supported Special Characters, such as adding single or double quotes with the appended content.

The following table contains all PowerShell special characters and their description:

Special Characters Description
`b Enters backspace.
`a For adding Alert.
`0 Used for Null.
`’ For adding a single Quote.
`“ For adding a Double Quote.
`t Use for Tabs.

We will now utilize some of the above-given Special Characters with the Add-Content command.

Example 1: Append data with Tab
In this example, we will append text in a file with a tab by using the special character `t:

> Add-Content -Path "E:\Powershell tutorial\New folder\Powershell4.txt" -value "`tIt is a command-line interface tool."

Output

Here, you will see that the appended text is started from a new line:

If you want to see the content in PowerShell rather than opening the file repeatedly, use the Get-Content cmdlet:

> Get-Content "E:\Powershell tutorial\New folder\Powershell4.txt

Output

Example 3: Append data with single and double quotations
We will now add single and double quotation marks in the appended text by using [`’ `“] PowerShell Special Characters:

> Add-Content -Path "E:\Powershell tutorial\New folder\Powershell4.txt" -value "It helps to `"automate`" `'tasks`' in the Windows ecosystem."

Here, you can see the appended text has single and double quotations:

Now, execute the Get-Content cmdlet:

> Get-Content "E:\Powershell tutorial\New folder\Powershell4.txt

Output

Example 4: Append data having multiple lines
We will now append multiple lines of data to a file using Add-Content cmdlet with special sign @:

> Add-Content -Path "E:\Powershell tutorial\New folder\Powershell4.txt" @"
>> Here see some cmdlets
>> Add-Content
>> Get-Content
>> "
@

The @ character helps to append multiple lines of text in a file:

Here, you can see that three lines are appended at a time in our selected text file:

For verifying appended data in PowerShell, use the Get-Content cmdlet:

> Get-Content "E:\Powershell tutorial\New folder\Powershell4.txt

Output

We have provided all of the relevant information related to appending data to a file using PowerShell.

Conclusion

To append data in a file using PowerShell, use the PowerShell built-in Add-Content cmdlet. The syntax of the command Add-Content is given as Add-Content [-Path] <sourcefile> [-Value] <content> in which -Path refers to the location of the file and the -Value indicates the appended data. You can also utilize PowerShell Special Characters with the Add-Content command to append data with different features such as having single or double quotes, tabs, and appending multiple lines at once. This blog discussed appending data to a file using the PowerShell Add-Content command.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.