EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PowerShell Append to File

Home » Data Science » Data Science Tutorials » PowerShell Tutorial » PowerShell Append to File

PowerShell Append to File

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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>]

Popular Course in this category
Sale
Shell Scripting Training (4 Courses, 1 Project)4 Online Courses | 1 Hands-on Project | 18+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (8,693 ratings)
Course Price

View Course

Related Courses
All in One Data Science Bundle (360+ Courses, 50+ projects)Data Visualization Training (15 Courses, 5+ Projects)

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:

PowerShell Append to File 1

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:

PowerShell Append to File 2

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:

PowerShell Append to File 3

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:

PowerShell Append to File 4

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.

PowerShell Append to File 5

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:

PowerShell Append to File 6

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:

PowerShell Append to File 7

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:

using Here-String command

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:

using Out-File command

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:

PowerShell Append to File 10

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:

Set-Content command

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.

CSV file data

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:

WinRM service information

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 –

  1. PowerShell Sleep
  2. PowerShell SubString
  3. PowerShell not like
  4. Else If in PowerShell

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PowerShell Tutorial
  • Basics
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Special Offer - Shell Scripting Course Learn More