EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial Add-Content in PowerShell
 

Add-Content in PowerShell

Priya Pedamkar
Article byPriya Pedamkar

Updated March 23, 2023

Add-Content in PowerShell

 

 

Introduction to Add-Content in PowerShell

One of the most underrated capacities or under the looked feature of any programming language is the I/O aspect or file handling mechanism. Similarly, in Add-Content in PowerShell, there are few contents that help in writing the output to a file or creating a new file or appending to an existing file. One such content is the Add-Content, content about which we will be seeing in detail in this article.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax: The easiest and simplest is as follows that is got from the help command.

Input: Get-Help Add-Content

Output:

add content in powershell

Parameters of Add-Content in PowerShell

Given below are some of the Parameters explained:

Parameters of Add-Content in PowerShell

1. Path: This refers to the path in which the file is present. It’s a mandatory parameter for the cmdlet to run. Wildcard characters are accepted. The default value is none and its type is a string.

2. Value: This refers to the content to be added. It’s a mandatory parameter for the cmdlet to run. Wildcard characters are not accepted. The default value is none and its type is the object.

3. Encoding: This parameter is used to denote the file encoding, this is an optional parameter. The default value for this ASCII. Wildcard characters are not accepted.

4. Force: This is also an optional parameter. The default value of this is false. This is required only to prevent the content from adding to read-only files. Wildcard characters are not accepted. Its type is the switch.

5. Confirm: This is also an optional parameter. This is used to just display a prompt for confirmation before executing the cmdlet. Wildcard characters are not accepted. Its type is a switch.

6. Exclude: This is also an optional parameter. Its default value is none. This is used to specify any path or files that need to be included. Wildcard characters are also accepted. Its type is a string.

7. Include: This is also an optional parameter. Its default value is none. This is used to specify any path or files that need to be included. Wildcard characters are also accepted. Its type is a string.

8. Filter: This is used to specify a filter for the path. It’s also an optional parameter. Wildcard characters are accepted, and the default value is none. Its type is a string.

9. LiteralPath: This is used to specify a path to one or more locations. This is also an optional parameter and its default value is none. This parameter doesn’t accept wildcard characters. Its type is a string.

10. NoNewline: This parameter is also optional. This denotes not to add a newline to the content. Its default value is none and wildcard characters are not accepted. Its type is the switch.

11. WhatIf: This parameter is optional. Its type is a switch and the default value is none. It doesn’t accept wildcard characters. It is used to see the output of the cmdlet when it is run.

12. UseTransaction: This parameter is optional. Its type is a switch and the default value is none. It doesn’t accept wildcard characters. It is used only when a transaction is running.

Sample Syntax 

Add-Content “file path” ‘Content”

E.g. Add-Content C:\testfolder\test.txt ‘adding content’

The above cmdlet appends the content specified to the test file if it exists, else it automatically creates the file and adds the content to it.

Examples of  the Add-Content in PowerShell

Below are some of the examples –

Example #1

Input: Add-Content -Path *. -Value "TestAppend"

Output:

The above command will add the value “test append” to all the text files in the current folder. To change the folder location run the below command

Set-Location -Path “directory path”

Example #2

Input: Add-Content D:\test\test1\test2.txt ‘example!'

Output:

The above command will add the content to the test2 file.

Example #3

Input: Add-Content 'C:\Vignesh\append.txt'  "date! $(Get-Date)"

Output:

In the above example, the current date and time are appended to the file.

Example #4

Input: Add-Content -Path a.txt, b.txt -Value (get-date)

Output:

The above cmdlet adds the current date and time to the text files.

Example #5

Input: Get-Content c:\source.txt | Add-Content D:\dest.txt

Output:

The above cmdlet appends the content to dest.txt to source.txt

Example #6

Input: Add-Content -Path source.txt -Value (Get-Content c:\destination.txt)

Output:

The above content first retrieves the content from destination.txt using the get-content. Then that value is passed to be added to the source.txt file.

Example #7

Input: Add-Content C:\vignesh\append.txt "`nLine1"
Add-Content C:\vignesh\append.txt "`nLine2"
Add-Content C:\vignesh\append.txt "`nLine3"
Add-Content C:\vignesh\append.txt "`nLine4"

Output:

Line1

Line2

Line3

Line4

The above would write the output each time in a new line because of a sequence.

To include tab space to the output content “`t” is used

Example #8

Input: Add-Content C:\vignesh\append.txt "`nsname`tm1`tm2`tm3"
Add-Content C:\vignesh\append.txt "`ns1`t4`t32`t28"
Add-Content C:\vignesh\append.txt "`ns2`t3`t50`t15"
Add-Content C:\vignesh\append.txt "`ns3`t12`t13`t84"

Output:

name   m1         m2         m3

s1           4             32           28

s2           3             50           15

s3           12           13           84

Example #9

Input: Add-Content -Value (Get-Content test.txt) -path C:\ram\test134.txt

Output:

In the above example, first, the value of the get-content is got by reading the test.txt file. That is pass as the value to be added to test134.txt. The ram folder doesn’t exist it is created on the fly.

It is also possible to use the Add-content to write content to a csv file. The following is an example that generates a csv file.

Example #10

Input:

Add-Content -Path C:\studentreport.csv  -Value '"Name","Subject1","Subject2","subject3","Total"'
$students = @(
'"ram",10,20,30,60'
'"mary",40,50,60,150'
'"peter",30,30,30,90'
'"john",30,30,30,90'
'"elizabeth",30,30,30,90'
'"John",40,50,60,150'
'"Shiv",40,50,60,150'
)
$students | foreach { Add-Content -Path  C:\studentreport.csv -Value $_ }

Output:

A csv file will be generated with the content as shown below

Name Subject1 Subject2 subject3 Total
ram 10 20 30 60
mary 40 50 60 150
peter 30 30 30 90
john 30 30 30 90
Elizabeth 30 30 30 90
John 40 50 60 150
Shiv 40 50 60 150

Conclusion

The Add-content is useful when the user needs to perform simple file operations to multiple files at the same time. This reduces the manual effort required and sometimes instead of checking if the file exists manually, the add-content will take care of creating the file if it doesn’t exist.

Recommended Articles

This is a guide to Add-Content in PowerShell. Here we discuss the Introduction, Parameters of Add-Content in PowerShell, and the Examples along with the Inputs and Outputs. You can also go through our other suggested articles to learn more–

  1. MATLAB Functions
  2. NOW Excel Function
  3. PowerShell String Functions
  4. Uses Of Powershell
  5. How to Declare and Initialize PowerShell Variables?
  6. Learn the Array in PowerShell
  7. PowerShell Get-Date | Examples | Parameters

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA

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

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

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

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

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

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW