Add-Content in PowerShell [With Real Examples]

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."
PowerShell Add-Content

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:

PowerShell Add-Content cmdlet

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." -NoNewline

This 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 UTF8

This 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 -Path parameter does not exist, Add-Content will create it automatically.
  • Appending to Read-Only or Hidden Files: Use the -Force parameter if you need to append to read-only or hidden files.
  • Automation: Add-Content is 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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.