• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Tachytelic.net

  • Sco Openserver
    • Sco Unix Support
    • SCO Openserver Installation ISOs
    • SCO Openserver Binaries
    • Add a Hard Drive to an Openserver 5 Virtual Machine
    • Install SCO Vision SQL-Retriever ODBC Driver on Windows 10
    • License Expired on Virtual SCO Openserver Installation
    • How to reset the root password on SCO Openserver 5
  • Scripting
    • PowerShell
      • Add leading zeros
      • Check if a File Exists
      • Grep with Powershell
      • Create Environment Variables
      • Test for open Ports
      • Append to a Text File
    • VBScript
      • Check if a File Exists
      • lpad and rpad functions
      • Windows Update E-Mail Notification
  • Office 365
    • Connect to Office 365 with PowerShell
    • Add or remove an email alias using Powershell
    • Change Primary email address of Active Directory user
    • How to hide an AD user from the Global Address List
    • How to hide mail contacts from the Global Address List
    • Change the primary email address for an account with PowerShell
    • Change Primary email address of an AD User
    • Grant a single user access to access to all calendars
    • Forward email to an external address using Powershell
    • Convert shared mailbox to user mailbox with Powershell
  • Get in Touch
  • About Me
    • Privacy Policy

How to append to a file with PowerShell

November 15, 2018 by Paulie 1 Comment

It’s easy to append text to a file with Windows Powershell with the Add-Content cmdlet, here are some examples:

  1. Create a sample text file using notepad. I have created mine like this:
    Image showing text file which will have text appended to it using Powershell Add-Content cmdlet
  2. Open a Powershell Window and type:
    Add-Content C:\temp\test.txt "Test"

    If you open the text file again, you will see the text has been appended on to the end of the existing line:
    Image showing how text file after text has been appended with Powershell Add-Content cmdlet

The above example is fine, but as you can see the data was not written to a new line.

Example PowerShell to append text to a file on to a new line

To append content on to a new line you need to use the escape character followed by the letter “n”:

So to continue the example above you could use:

Add-Content C:\temp\test.txt "`nThis is a new line"

And then the resulting text file would look like this:

Image of a text file which has had data appended to it on a new line using Windows Powershell Add-Content cmdlet

Append formatted data with Powershell by using tabs and new lines

This example creates a tab formatted file, the output looks like this:

Image showing how to format a text document using tabs and newline with powershell Add-Content cmdlet

The Powershell to create the above example is:

Add-Content C:\temp\test.txt "`nItem`tQty`tValue`tTotal"
Add-Content C:\temp\test.txt "`nPants`t4`t32.22`t128.88"
Add-Content C:\temp\test.txt "`nSocks`t3`t5.07`t15.21"
Add-Content C:\temp\test.txt "`nShoes`t12`t136.57`t1638.84"

As you can see, the tabs are added with the special character “`t”.

Appending to a file using redirection

The process described above seems to be the “normal” way to append to a file in PowerShell. But if you have come from a Unix or Linux background that will probably seem like hard work compared to just using command redirection, which also works perfectly well in PowerShell. For example:

"This is a test" >> Testfile.txt

I don’t know why this method isn’t used more in PowerShell because it is much more succinct than “Add-Content”. Perhaps it is because it does not have the same level of functionality, in most cases it is fine.

Related

Filed Under: How To Tagged With: Powershell

Reader Interactions

Comments

  1. ray says

    December 23, 2019 at 3:54 pm

    awesome! thanks!

Leave a Reply Cancel reply

Primary Sidebar

Link to my LinkedIn Profile
Go to mobile version