If you’re just getting started with PowerShell, one of the first things you’ll want to learn is how to write data to a file. That’s where the PowerShell Add-Content cmdlet comes in.
In this tutorial, you’ll learn everything you need to know about the Add-Content cmdlet in PowerShell: what it is, how it works, its syntax, and practical examples.
What Is Add-Content in PowerShell?
Add-Content is a built-in PowerShell cmdlet used to append text to a file. This means instead of replacing or overwriting the content, it simply adds new lines or data at the end of an existing file. If the file doesn’t exist, PowerShell automatically creates it for you, provided the folder path already exists.
Think of a notebook. Each time you use Add-Content, you’re adding a new note at the end without erasing any previous pages.
This cmdlet is often used in:
- Logging application events
- Capturing user input for audits or feedback
- Writing system reports during automation scripts
- Creating simple data collection forms
Check out PowerShell Test-NetConnection
Syntax of the PowerShell Add-Content Cmdlet
Here is the basic syntax for the Add-Content cmdlet in PowerShell:
Add-Content -Path <FilePath> -Value <String>Parameters:
- -Path: Specifies the path to the file you want to append content to.
- -Value: The content you want to add.
- -Encoding: (Optional) Specifies the file encoding (UTF8, ASCII, etc.).
- -Force: (Optional) Allows writing to read-only files or hidden files.
- -NoNewline: (Optional) Prevents adding a new line after the content.
You can also pipe content to Add-Content:
"Hello, World!" | Add-Content -Path "C:\example\log.txt"Example:
Add-Content -Path "C:\Bijay\file.txt" -Value "Your text here"Read PowerShell Invoke-WebRequest
PowerShell Add-Content Examples
Now, let me show you some examples of using the Add-Content cmdlet in PowerShell, starting from a basic one.
Example 1: Append a Simple String to a File
Let’s start with a basic example. Suppose you have a file called log.txt, and you want to add the line “Script executed successfully.”
Add-Content -Path "C:\Reports\log.txt" -Value "Script executed successfully."After running this command, the line will be added to the end of log.txt.
Here is the cmdlet; you can see I am running the command on my Mac laptop.
Add-Content -Path "/Users/bijay/Documents/Reports/log.txt" -Value "Script executed successfully."
Check out PowerShell Out-File
Example 2: Append Multiple Lines to a File
You can append several lines at once by passing an array of strings to the Add-Content cmdlet:
Add-Content -Path "C:\Reports\log.txt" -Value @(
"Process started at $(Get-Date)",
"Step 1 completed.",
"Step 2 completed.",
"Process ended at $(Get-Date)"
)Each string in the array will be added as a new line in the file.
I have executed the PowerShell command below on Mac OS.
Add-Content -Path "/Users/bijay/Documents/Reports/log.txt" -Value @(
"Process started at $(Get-Date)",
"Step 1 completed.",
"Step 2 completed.",
"Process ended at $(Get-Date)"
)You can see the exact output in the screenshot below:

Check out PowerShell Remove-Item Cmdlet
Example 3: Append Output from a Command
You can append the output of another command directly to a file. For example, to log the list of running processes:
Get-Process | Out-String | Add-Content -Path "C:\example\processlog.txt"This command gets all running processes, converts the output to a string, and appends it to processlog.txt.
Example 4: Append Data Without a New Line
By default, Add-Content adds a new line after the content. If you want to append text without a new line, use the -NoNewline parameter:
Add-Content -Path "C:\example\log.txt" -Value "No new line here." -NoNewlineThis will add the text directly after the previous content, without starting a new line.
Example 5: Specify File Encoding
If you need to ensure the file uses a specific encoding (for example, UTF8), use the -Encoding parameter:
Add-Content -Path "C:\example\utf8log.txt" -Value "This is a UTF8 encoded line." -Encoding UTF8This is useful when working with non-English characters or when the file will be used by other applications that require a specific encoding.
Read PowerShell Greater Than or Equal
Example 6: Append Content Using the Pipeline
PowerShell allows you to use the pipeline to pass data between commands. For example, you can filter event logs and append them to a file:
Get-EventLog -LogName System -Newest 5 | Out-String | Add-Content -Path "C:\example\eventlog.txt"This command gets the 5 newest entries from the System event log and appends them to eventlog.txt.
Best Practices while using the PowerShell Add-Content Cmdlet
I am sharing some best practices to follow when working with the Add-Content cmdlet in PowerShell.
- Creating a File if it Doesn’t Exist: If the file specified in the
-Pathparameter does not exist,Add-Contentwill create it automatically. - Appending to Read-Only or Hidden Files: Use the
-Forceparameter if you need to append to read-only or hidden files. - Automation:
Add-Contentis perfect for automation scripts where you need to log progress or results.
Conclusion
The Add-Content cmdlet is used to append new content to the end of an existing file. Unlike Set-Content, which replaces the entire file content, Add-Content preserves the current contents, and simply adds the specified data at the end.
In this tutorial, I explained how to work with Add-Content in PowerShell with various examples. Do let me know in the comments below if it is helpful to you.
You may also like the following tutorials:
- PowerShell Format-List
- PowerShell Select-String -AllMatches Examples
- PowerShell Select-String Plus Next Lines
- PowerShell Select-String Exact Match
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.