Append Information to Text Files

Add-Content is a versatile command if you need to add additional content to text files. For example, you can create log file entries like this:

Add-Content $home\logfile.txt ("{0:dd MM yyyy HH:mm:ss} New Entry" `
-f (Get-Date))
& "$home\logfile.txt"

But there is more. Use the -passThru parameter if you'd like to see what exactly Add-Content is adding. This way, Add-Content also sends the value to the console:

Add-Content $home\logfile.txt ("{0:dd MM yyyy HH:mm:ss} New Entry" `
-f (Get-Date)) -passThru

You should be aware that Add-Content happily accepts wildcards. The next line would add your log file entry to every single log file in your home folder (make sure there are some before you try):

Add-Content $home\*.log ("{0:dd MMM yyyy HH:mm:ss} New Entry" `
-f (Get-Date)) -whatIf

Posted May 20 2009, 08:00 AM by ps1
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.