in

PowerShellPowerShell

[WORKING] Append text without deleting using PowerShell – 2020

Learn how to append content to a file without removing the already exisitng content in PowerShell. It is clear that we can powershell append to file here.

PowerShell append content to file

In this post, we are going to see how to append some text to a file.  We will add text without deleting old content.  This is very useful for creating system logs in PowerShell.  There are several different ways in which we can write the content to a file and appending the content is very important.  For appending the content, we will use the Add-Content commandlet.

Jump to:

People Also Search For:

PowerShell Append text to file:

The add-content will append the content to the file.  Here is a basic example.

notepad teckangaroo.com

create new file

The above script will create a file called teckangaroo.com and we will add one word hello.  We will close the file and continue with the following script.

add-content -path .\teckangaroo.com -Value ” world”

append content

This script will add another word to the file without deleting the original content.  We will view the content and check.

notepad.exe .\teckangaroo.com

The output of the file is shown below.  Clearly, this appends the text of the file without deleting the contents.

append content using powershell

PowerShell create and add content to a file:

We can add the contents from an existing file to a new file by creating it.  The following script will create a new file and copy the contents to the new file and open it.

Add-Content -path newfile -Value $(Get-Content “.\teckangaroo.com”)

notepad .\newfile

The above script will have the following output.

append content from a file

PowerShell add content to a read-only file:

We can use the -force switch to add content to even read-only files.  Consider a file that is read-only named readonlyfile.txt.  We can add content to the file with the help of -force switch as shown below.

Add-Content readonlyfile.txt -Value “new content” -force

force append

PowerShell appends content alias:

The add-content as an alias called ac.  This can be used as shown below to append content to an existing file.

ac .\teckangaroo.com -v “new value”

notepad.exe .\teckangaroo.com

add content alias

PowerShell add-content flags:

Apart from the above examples, we can do other useful things using the add-content with the help of different flags.

`a — Alert
`b — Backspace
`n — New line
`r — Carriage return
`t — Horizontal tab
`’ — Single quote
`” — Double quote

Please note that we have to use the above commands wrapped inside quotes.

PowerShell appends to file using file redirection:

This is perhaps the easiest syntax to append content to the file.  This involves only the redirection stream syntax as shown below.

“redirect to file” >> .\teckangaroo.com

The above script will append the contents to the file teckangaroo.com

Verdict:

In this post, we learned how to append text to file using PowerShell.  We use the add-content commandlet for this purpose.  There is another easy way to append text to a file using the redirect to file syntax.

If you want to know more about a redirect to file, stay tuned to TecKangaroo.

Leave a Reply

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