As a PowerShell user, I’ve found that one of the most common tasks is writing data to files, especially appending content to existing files without overwriting previous information. This will be helpful in scenarios such as creating logs, generating reports, or saving script output, etc.
In this tutorial, I’ll walk you through the different methods to append text to files in PowerShell, with clear examples.
Now, let’s explore the primary methods for appending text to files in PowerShell.
Method 1: Using Add-Content Cmdlet
The Add-Content cmdlet is the best way to append text to a file in PowerShell. It’s specifically designed to add content to the end of a file without disturbing existing content.
This method automatically creates the file if it doesn’t exist. Additionally, it does not overwrite existing content and can append multiple values at once.
Basic Syntax
Add-Content -Path "C:\Bijay\file.txt" -Value "Text to append"
Practical Example
Here is an example of appending text to a file in PowerShell.
# Creating a simple log entry
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logEntry = "$timestamp - Backup process completed successfully."
Add-Content -Path "C:\Logs\backup-log.txt" -Value $logEntry
Here is the exact output in the screenshot below:

Here is another advanced usage example.
You can also pipe content directly to Add-Content:
"New log entry for $(Get-Date)" | Add-Content -Path "C:\Logs\system-log.txt"
For multiple lines, you can use an array:
$newLines = @(
"Line 1 to append"
"Line 2 to append"
"Line 3 to append"
)
Add-Content -Path "C:\Reports\daily-report.txt" -Value $newLines
Check out PowerShell Write to File
Method 2: Using Out-File with the Append Parameter
The Out-File cmdlet in PowerShell is another popular option for writing to files, and it includes an -Append parameter for adding content without overwriting.
Syntax
Here is the syntax:
"Text to append" | Out-File -FilePath "C:\path\to\file.txt" -Append
Practical Example
Let me show you an example.
# Appending system information to a report
$computerInfo = "Computer Name: $env:COMPUTERNAME"
$computerInfo | Out-File -FilePath "C:\Reports\system-info.txt" -Append
# Adding more information
"OS Version: $((Get-CimInstance Win32_OperatingSystem).Version)" | Out-File -FilePath "C:\Reports\system-info.txt" -Append
When to Use Out-File Instead of Add-Content
While both methods append content, Out-File has some specific advantages:
- Provides better control over encoding with the
-Encodingparameter - Allows setting the file width with
-Width - Can force use with the
-Forceparameter (overriding read-only attributes)
Get-Process | Out-File -FilePath "C:\Reports\processes.txt" -Append -Width 120 -Encoding UTF8
Check out PowerShell Global Variables
Method 3: Using the >> Redirection Operator
PowerShell includes a redirection operator (>>) that provides a quick shorthand for appending content to files.
Syntax
Here is the syntax:
"Text to append" >> "C:\path\to\file.txt"
Practical Example
You can see the example below.
# Simple logging using redirection
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"$timestamp - User $env:USERNAME logged in." >> "C:\Logs\user-activity.txt"
I executed the above PowerShell script, and you can see the exact output in the screenshot below:

Pros and Cons of Redirection
From my experience, let me tell you some pros and cons of Redirection in PowerShell.
Pros:
- Shortest syntax of all methods
- Familiar to those with command-line experience
- Quick for simple appending tasks
Cons:
- Less control over encoding and other options
- Not as PowerShell-native as the cmdlet methods
- Can be trickier to use with complex data structures
Check out Create a Log File using PowerShell
Method 4: Using .NET Methods for Advanced Scenarios
For more advanced control, you can use .NET methods for appending to files. This approach gives you the most granular control but requires more code.
Syntax
Below is the syntax:
[System.IO.File]::AppendAllText("C:\path\to\file.txt", "Text to append`r`n")
Practical Example
Now let me show you a practical example.
# Using .NET methods for controlled appending
$filePath = "C:\Logs\application-log.txt"
$textToAppend = "$(Get-Date) - Application started with parameters: $params`r`n"
[System.IO.File]::AppendAllText($filePath, $textToAppend)
For even more control, you can use a StreamWriter:
$filePath = "C:\Logs\detailed-log.txt"
$streamWriter = [System.IO.StreamWriter]::new($filePath, $true)
$streamWriter.WriteLine("Log entry at $(Get-Date)")
$streamWriter.WriteLine("System information: $env:COMPUTERNAME running PowerShell $($PSVersionTable.PSVersion)")
$streamWriter.Close()
When to Use .NET Methods
- When you need precise control over the file operation
- For handling special encoding requirements
- When working with very large files where performance matters
- If you’re integrating with existing .NET code
Check out PowerShell Copy-Item Cmdlet to Copy Files and Folders
Comparison of Various Append Methods in PowerShell
Here’s a quick comparison table to help you choose the right method for your needs:
| Method | Ease of Use | Control | Performance | Best For |
|---|---|---|---|---|
| Add-Content | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ | ⭐⭐⭐ | Everyday appending tasks, logs |
| Out-File -Append | ⭐⭐⭐⭐ | ⭐⭐⭐⭐ | ⭐⭐⭐ | Formatted output, controlling width |
| >> Operator | ⭐⭐⭐⭐⭐ | ⭐⭐ | ⭐⭐⭐⭐ | Quick one-liners, simple text |
| .NET Methods | ⭐⭐ | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ | Advanced scenarios, performance-critical code |
Practical Use Cases for Appending to Files
As a developer, you should know some practical user cases for appending to files in PowerShell.
Let me show you two or three real use cases.
Create Application Logs
Here is the complete code for application logs.
function Log-Message {
param (
[string]$Message,
[string]$LogPath = "C:\Logs\application.log"
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"$timestamp - $Message" | Add-Content -Path $LogPath
}
# Using the function
Log-Message "Application started"
Log-Message "Processing data files"
Log-Message "Operation completed successfully"
Track Script Execution
Here is another real example.
# At the beginning of your script
$scriptLog = "C:\Scripts\Logs\script-execution.log"
"$(Get-Date) - Script started by $env:USERNAME on $env:COMPUTERNAME" | Add-Content -Path $scriptLog
# Later in your script
"$(Get-Date) - Processing completed with 23 items" | Add-Content -Path $scriptLog
# At the end
"$(Get-Date) - Script completed successfully" | Add-Content -Path $scriptLog
Append CSV Data
Here is another example of appending CSV data.
For structured data, you might want to append to CSV files:
$newUser = [PSCustomObject]@{
Username = "jsmith"
FullName = "John Smith"
Department = "Marketing"
Created = Get-Date
}
# Export as CSV and append
$newUser | Export-Csv -Path "C:\Data\users.csv" -Append -NoTypeInformation
Check out Check if a File Exists and Rename it Using PowerShell
Best Practices for Appending to Files
After years of working with PowerShell, I’ve developed these best practices for file operations:
- Check if the file exists before appending
if (-not (Test-Path -Path $filePath)) {
# Create the file with headers or initial content
"Timestamp,Action,Status" | Out-File -FilePath $filePath
}
# Now append to the file
"$((Get-Date).ToString()),DataProcess,Success" | Add-Content -Path $filePath
- Always close file handles when using .NET methods
$stream = $null
try {
$stream = [System.IO.StreamWriter]::new($filePath, $true)
$stream.WriteLine("Data to append")
}
finally {
if ($stream) { $stream.Close() }
}
- Consider encoding requirements
Add-Content -Path $logFile -Value $logEntry -Encoding UTF8
- Handle errors gracefully
try {
"Log entry" | Add-Content -Path "C:\Logs\app.log" -ErrorAction Stop
}
catch {
Write-Warning "Could not write to log file: $_"
}
- Use locking when multiple processes might write to the same file
$mutex = New-Object System.Threading.Mutex($false, "Global\MyAppLogMutex")
try {
$mutex.WaitOne() | Out-Null
Add-Content -Path $logFile -Value $logEntry
}
finally {
$mutex.ReleaseMutex()
}
Read Rename Multiple Files Using PowerShell
Troubleshooting Common Issues
I thought to share with you two very common issues you will get while appending to files using PowerShell.
Problem: Access Denied Errors
If you encounter permission issues:
# Try running with elevated permissions
# Or explicitly set permissions on the file
$acl = Get-Acl -Path $filePath
$permission = "DOMAIN\Username", "FullControl", "Allow"
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
$acl.SetAccessRule($accessRule)
Set-Acl -Path $filePath -AclObject $acl
Problem: File Locked by Another Process
If the file is being used by another process, then you might receive an error while trying to append to the file.
# Option 1: Use robust error handling
try {
Add-Content -Path $filePath -Value $content -ErrorAction Stop
}
catch {
Start-Sleep -Seconds 2
# Try again
Add-Content -Path $filePath -Value $content
}
# Option 2: For critical logs, use alternative file
if (-not (Test-Path -Path $filePath -PathType Leaf)) {
Add-Content -Path "$filePath.alt" -Value $content
}
Conclusion
In this tutorial, I explain how to append to files in PowerShell using various methods. I recommend using the Add-Content method to append to files in PowerShell.
You may also like:

Hey! I’m Bijay Kumar, founder of SPGuides.com and a Microsoft Business Applications MVP (Power Automate, Power Apps). I launched this site in 2020 because I truly enjoy working with SharePoint, Power Platform, and SharePoint Framework (SPFx), and wanted to share that passion through step-by-step tutorials, guides, and training videos. My mission is to help you learn these technologies so you can utilize SharePoint, enhance productivity, and potentially build business solutions along the way.