While working with files in PowerShell, we have to append text to a file using PowerShell like add text to the end of a file, append a string to a file, add content from a different file or append DateTime to the end of the file.
Add-Content cmdlet in PowerShell add content to file, you can specify content using content in the command or using Get-Content to get content of the file for append.
Using the Add-Content cmdlet, it appends text to the file or appends a string to the file.
In this blog post, I will explain PowerShell Add-Content with different examples to append content to file.
Let’s practice with PowerShell Add-Content Examples.
Add-Content cmdlet in PowerShell appends content to file such as appends text to file.
Add-Content [-Path] <string> [-Value] <Object> [-PassThru] [-Filter <string>] [-Include <string>] [-Exclude <string>] [-Force] [-Credential <pscredential>] [-WhatIf] [-Confirm] [-NoNewline] [-Encoding <Encoding>] [-AsByteStream] [-Stream <string>] [<CommonParameters>]
Append text to file using Add-Content
Let’s consider an example, we have the Get-DateFile.txt file in the directory.
Let’s append the data to a text file using PowerShell, create a new file as Get-DateFile.txt, and add some data to it.
Add-Content -Path .\Get-DateFile.txt "End of file"
In the above example, Add-Content appends the “End of file” text data to the end of the file specified by the Path parameter in the current directory.
The output after adding data to the file is:
Append data to text file on new line
To append data to a text file on a new line, use `n ( PowerShell new line).
Considering the above example, to add text to file on a new line, use
Add-Content -Path .\Get-DateFile.txt "`nSecond para starts here..."
In the above PowerShell script, we have used `n to add PowerShell new line before the text. It will write a line to file on a new line.
The output of the above script is:
Add datetime to the end of file
Using the Add-Content cmdlet in PowerShell, you can add DateTime to the end of the file as given below
Add-Content -Path .\Get-DateFile.txt -Value (Get-Date)
In the above example, Add-Content appends date time to text file specified by the Path parameter. The Value parameter specifies the string written to the file.
Cool Tip: How to use PowerShell Copy-Item to copy File in PowerShell!
Add Content of one file to another file
If you want to append the content of one file to another file, you can do it easily using the Add-Content cmdlet.
It will read file contents to a variable and using Add-Content, will write content to the destination file.
This example demonstrates to get content from one file and append it to another file using the PowerShell cmdlet.
Add-Content will create a new file if the file does not exist while adding the text to the file.
# Read file contents to variable $sourceFileContent = Get-Content -Path .\GetFileProperties-Assignment.txt # Appends contents of one file to another file # If the file does not exist, Add-Content create a new file Add-Content -Path .\DestinationFile.txt -Value $sourceFileContent
In the above example to append content of one file to another file,
Get-Content cmdlet in PowerShell gets the content of file specified by the Path parameter. It reads file contents and stores them in a variable $sourceFileContent.
Add-Content cmdlet in PowerShell appends the content of the source file specified in the Value parameter.
The Add-Content cmdlet will create a new file if the file does not exist and copy the content.
Append data to read only file
Using the Add-Content cmdlet in PowerShell, it appends content to a read-only file as well.
Let’s understand to append data to a read-only file using the example…
# Create new file New-Item -Path .\TempReadonlyFile.txt -ItemType File # Set file as read only Set-ItemProperty -Path .\TempReadonlyFile.txt -Name IsReadOnly -Value $True # Get file details Get-ChildItem -Path .\TempReadonlyFile.txt # Appends the line to file Add-Content -Path .\TempReadonlyFile.txt -Value 'End of File' -Force
In the above example to append a line to a file,
The first command creates a new file using the New-Item cmdlet in PowerShell.
Set-ItemProperty cmdlet in PowerShell to set IsReadOnly property to true for the specified file
Get-ChildItem cmdlet in PowerShell gets specified file details as mode, LastWriteTime, Length, and Name
Add-Content cmdlet appends a line to a read-only file specified by the Path parameter.
The output of the above script to append a line to a read-only file is:
Cool Tip: How to use Clear-Content to clear content from a text file in PowerShell!
I hope the above blog post on Add-Content to append text to file is helpful to you.
Add-Content cmdlet in PowerShell append multiple lines from another file to the source file, add DateTime to the end of the file, and append text to a read-only file.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.