Append Information to Text Files

by May 20, 2009

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 $homelogfile.txt (“{0:dd MM yyyy HH:mm:ss} New Entry” `
-f (Get-Date))
& “$homelogfile.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 $homelogfile.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