Updated March 6, 2023
Introduction to PowerShell Append to File
The following article provides an outline for PowerShell Append to File. PowerShell appends to the file operation is the way to add the content to the different types of file like TXT, CSV, Excel, JSON, etc. by using the various cmdlets like Out-File, Add-Content, Export-CSV, etc. and the various methods on the existing file by either adding the new line to the content or by appending data to the continuation of the last line of the file.
Syntax of PowerShell Append to File
There are various cmdlets used to append the content to the existing file, but they all are not the direct cmdlets or syntaxes, but they use the parameters which help to append the file.
1. Add-Content cmdlet.
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>]
2. Out-File cmdlet.
Out-File
[-FilePath] <string>
[[-Encoding] <Encoding>]
[-Append]
[-Force]
[-NoClobber]
[-Width <int>]
[-NoNewline]
[-InputObject <psobject>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
There are other files like CSV, which has a separate cmdlet to append the content to the file.
Export-Csv
-InputObject <PSObject>
[[-Path] <String>]
[-LiteralPath <String>]
[-Force]
[-NoClobber]
[-Encoding <Encoding>]
[-Append]
[[-Delimiter] <Char>]
[-IncludeTypeInformation]
[-NoTypeInformation]
[-QuoteFields <String[]>]
[-UseQuotes <QuoteKind>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
We can use -Append parameter from the Out-File and Export-CSV cmdlet to append the content to the file.
How does PowerShell Append to File Works?
Appending the file or adding the content to the file in PowerShell or other programming language is not that much tough. In PowerShell, there are various cmdlets and their parameters supported to append the content to the existing file.
We have a file name called test.txt stored at the C:\Temp, and we have its content as below.
Code:
Get-Content C:\Temp\test.txt
Output:
To append the line, we will use the double arrow (>>) that is the basic syntax and which is the most common method in .Net methods.
Code:
"This is the fourth line" >> C:\Temp\test.txt
Get-Content C:\Temp\test.txt
Output:
The last line was appended to the new line. Instead of appending it to the last one, add the new line, use the carriage (`n).
Code:
"`nThis is the fourth line" >> C:\Temp\test.txt
Get-Content C:\Temp\test.txt
Output:
If you have more than one line to add, you can use directly append, or you can use the string variable.
Code:
$str = "`nThis is the fourth line.`nThis is the 5th line"
$str >> C:\Temp\test.txt
Get-Content C:\Temp\test.txt
Output:
But this method is not recommended because when you check the file in the other editors (use notepad++), you can see the null values are added. The below snapshot is from the Notepad++ editor.
There are other methods like Out-File, Add-Content, etc., to append the content, and they are shown in the examples below.
Examples of PowerShell Append to File
Given below are the examples of PowerShell Append to File:
Example #1
Append the file with the Add-Content cmdlet.
We will use the same file test.txt to append the content of the file using the Add-Content cmdlet.
Code:
"This is the 4th line" | Add-Content -Path C:\Temp\test.txt
Or
Add-Content -Value "This is the 4th line" -Path C:\Temp\test.txt
Get-Content C:\Temp\test.txt
Output:
Add the multiple lines using a variable.
Code:
$str = "`nThis is the 4th line. `nThis is the 5th line. `nThis is the 6th line"
$str | Add-Content -Path C:\Temp\test.txt
Or
Add-Content -Value $str -Path C:\Temp\test.txt
Get-Content C:\Temp\test.txt
Output:
Example #2
Appending the content using the Here-String command.
The best way to append the multiple lines of strings using the here-string @”..”@ method is shown below.
Code:
$str = @"
`nThis is the 4th line
This is the 5th line
This is the 6th line
"@
$str | Add-Content C:\Temp\test.txt
Get-Content C:\Temp\test.txt
Output:
Example #3
Appending file using Out-File command.
We can use the Out-File command with the -Append parameter to add the content to the file.
Code:
$str = @"
`nThis is the 4th line
This is the 5th line
This is the 6th line
"@
$str | Out-File -FilePath C:\Temp\test.txt -Append -Force
Get-Content C:\Temp\test.txt
Output:
Here, you will get the output similar to the >> output because of the encoding problem, and when you check the output file with Notepad++ editor, they are filled with the $null value. We don’t want that, so instead, we can use the encoding method supported by this cmdlet using -encoding parameter.
You can use standard ASCII or utf8 encoding standard to get the proper output format.
Code:
$str | Out-File -FilePath C:\Temp\test.txt -Append -Encoding ascii -Force
Output:
Make sure to use -Append parameter; otherwise, the file will be overwritten.
Example #4
Using the Set-Content command to add the content.
We can also use the Set-Content command to append the file, but this is not the standard command for file append operation because we need to overwrite the entire content after adding the new content in the existing file.
Code:
$str = @"
This is the 4th line
This is the 5th line
This is the 6th line
"@
$Inputfile = 'C:\Temp\test.txt'
$file = Get-Content $Inputfile
$file + $str | Set-Content $Inputfile -Force
Get-Content $Inputfile
Output:
Example #5
Appending the CSV file data.
If you already have the CSV file, then appending it to the CSV file is easy using Export-CSV cmdlet with -Append parameter.
We have the below CSV file present at C:\temp\Services.csv, and we want to append the data to it.
If you want to append more service, let say WinRM service information, then you can use the below command.
Code:
Get-Service Winrm | Select Name, DisplayName, Status, StartType | Export-Csv C:\Temp\services.csv -Append -Force
Output:
Conclusion
Adding content to the file or appending file operations are one of the most useful and frequently used operations but one of the painful tasks to manually do all this stuff and provide the data to the end-users like finance, HR, legal department, etc. but using these cmdlets we can also automate and schedule the file update task without any user intervention.
Recommended Articles
This is a guide to PowerShell Append to File. Here we discuss the introduction; how does PowerShell append to file works? And examples. You may also have a look at the following articles to learn more –