Bootstrap FreeKB - PowerShell - Append to a file using Add-Content
PowerShell - Append to a file using Add-Content

Updated:   |  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 Buy Me A Coffee



Comments


Add a Comment


Please enter 173ec9 in the box below so that we can be sure you are a human.