PowerShell Genie

Add-Content

Use Add-Content to append a value to the end of a file, leaving existing content in place.

Command 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn — Add-Content

Syntax

1PS> Add-Content [-Path] <String[]> [-Value] <Object[]> [-Encoding <Encoding>] [-Force] [-NoNewline] [-WhatIf] [<CommonParameters>]
See the command anatomy

Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.

Add-Content [-Path] <String[]> [-Value] <Object[]> [-Encoding <Encoding>] [-Force] [-NoNewline] [-WhatIf] [<CommonParameters>]
  1. Stage 1Add-Content-Path -Value -Encoding -Force -NoNewline -WhatIf

    Try it

    Windows PowerShell

    PowerShell example · browser support is limited.

    Execution scope: The full example below requires PowerShell outside this browser. You can edit this runner to use the supported practice commands; unsupported syntax returns an explanation.

    Interactive simulation · fictional data

    Examples

    Add-Content -Path 'C:\Temp\run.log'

    1PS> Add-Content -Path 'C:\Temp\run.log' -Value 'step 1 complete' -Encoding utf8
    Show reference output

    Sample output · fictional data, shaped like the real thing

    2026-10-09 14:31:02 INFO  Backup started
    2026-10-09 14:31:07 INFO  42 files copied
    step 1 complete

    Add-Content -Path 'C:\Temp\run.log' (2)

    1PS> Add-Content -Path 'C:\Temp\run.log' -Value ((Get-Date -Format 'HH:mm:ss') + ' started') -Encoding utf8
    Show reference output

    Sample output · fictional data, shaped like the real thing

    2026-10-09 14:31:02 INFO  Backup started
    2026-10-09 14:31:07 INFO  42 files copied
    step 1 complete
    14:32:10 started

    Add-Content -Value $lines;

    1PS> $lines = 1..3 | ForEach-Object { "Line $_" }; Set-Content C:\Temp\app.log -Value $lines; Get-Content C:\Temp\app.log
    Show reference output

    Sample output · fictional data, shaped like the real thing

    2026-09-10 23:41:02 INFO  Backup started
    2026-09-10 23:41:07 INFO  42 files copied
    2026-09-10 23:41:08 INFO  Backup completed

    Parameters

    Parameter definitions:

    • -Path
    • -Value
    • -Encoding
    • -Force
    • -NoNewline
    • -WhatIf

    Add-Content on Microsoft Learn

    Practical use and troubleshooting

    Practical scenario: Use Add-Content when you need to append a value to the end of a file, leaving existing content in place. Start with one identified target and inspect the returned values before extending the operation to a list. Keep the identity in your report so you can trace each result back to the object or path that produced it.

    Common error and fix: Access denied when appending usually points to file permissions or a file held open by another process. Check the target ACL and the owning application before retrying. In an unattended script, capture this failure with its original error record and record which target failed. An empty result and an unsuccessful query must remain distinguishable in the final report.

    See also: Set-Content. Add-Content appends to existing content; Set-Content replaces it. Choose deliberately when maintaining a log rather than creating a fresh report. Read the linked command before substituting it; similar names do not imply identical inputs or output.

    Add-Content reference on Microsoft Learn

    Recap

    Add-Content Use Add-Content to append a value to the end of a file, leaving existing content in place.

    Next: Clear-Content
    Genie guideAdd-Content

    Use Add-Content to append a value to the end of a file, leaving existing content in place. The console below is pre-loaded — run it, then change a value and run it again.