PowerShell - Append to a file using Add-Content

by
Jeremy Canfield |
Updated: June 09 2023
| PowerShell articles
Test-Path can be used to determine if a files exists. Add-Content can be used to append lines to a file.
$file = 'C:\Users\JohnDoe\example.txt'
if (Test-Path -Path $file) {
Write-Host "$file exists"
try {
Add-Content $file "Hello"
Add-Content $file "World"
} catch {
Write-Warning $Error
}
}
else {
Write-Host "$file does NOT exist"
}
Did you find this article helpful?
If so, consider buying me a coffee over at