Append content to a specified item or file. You can specify the content by typing the content in the command or by specifying an object that contains the content.

      Add-Content [-Value] Object[] [-Credential PSCredential]
         [-Encoding {Unknown | String | Unicode | Byte | BigEndianUnicode | UTF8 | UTF7 | UTF32 | Ascii | Default | Oem | BigEndianUTF32}]
            [-Exclude string[]] [-Filter string] [-Force] [-Include string[]] -LiteralPath string[]
               [-NoNewline] [-PassThru] [-Stream string] [-UseTransaction]
                  [-Confirm] [-WhatIf] [CommonParameters]

      Add-Content [-Path] string[] [-Value] Object[]  [-Credential PSCredential]
         [-Encoding {Unknown |  String | Unicode | Byte | BigEndianUnicode | UTF8 | UTF7 | UTF32 | Ascii | Default | Oem | BigEndianUTF32}]
            [-Exclude string[]] [-Filter string] [-Force] [-Include string[]]
               [-NoNewline] [-PassThru] [-Stream string] [-UseTransaction]
                  [-Confirm] [-WhatIf] [CommonParameters]

      Add-Content [-Encoding {Unknown | String | Unicode | Byte | BigEndianUnicode | UTF8 | UTF7 | UTF32 | Ascii | Default | Oem}]
         [-Force] [-Stream string]
            [-Confirm] [-WhatIf] [-UseTransaction] [CommonParameters]

   -Path path
       The path to the item {may be piped} Wildcards are permitted.
       If you specify multiple paths, use commas to separate the paths.

   -LiteralPath string[]
       Path to the items that receive the additional content. No wildcards.
       The value is used exactly as typed. 
       If the path includes escape characters, enclose it in single quotation marks.

   -Include string[]
       Qualify the Path parameter. Enter a path or wildcard pattern: "*.txt"

   -Exclude string[]
       Omit the specified items. Enter a path or wildcard pattern: "*.txt" 

   -Filter string[]
       A filter in the provider's format or language.
       The exact syntax of the filter (wildcard support etc) depends on the provider.
       Filters are more efficient than -include/-exclude, because the provider
       applies the filter when retrieving the objects, rather than having 
       PowerShell filter the objects after they are retrieved.     
  -Value Object[]
       The content to be added. Type a quoted string, such as "Sample text"
       or specify an object that contains content, such as the DateTime object
       that Get-Date generates. 
       You cannot specify the contents of a file by typing its path, because 
       the path is just a string, but you can use a Get-Content command to get 
       the content and pass it to the Value parameter.

       Pass the object created by this cmdlet through the pipeline.

       Override restrictions that prevent the command from succeeding, just
       so the changes do not compromise security. For example, -Force will
       override the read-only attribute but it will not attempt to change file permissions.
       Add-Content will create directories to complete a path even without the -Force parameter.

       Do not add a new line/carriage return to the content.
       This parameter is not supported by any providers installed with PowerShell.

   -Credential PSCredential
       Present a user/password credential to validate access to the file.
       This is not yet supported in any Windows PowerShell core commands.

       Prompt for confirmation before executing the command.

       Describe what would happen if you executed the command without actually executing it.

       Include the command in the active transaction.

   -Encoding CharSet[Dynamic Parameter (FileSystem Only)]
       Encode in a specific character set:
           ASCII             ASCII (7-bit) character set.
           Byte              Encode characters as a sequence of bytes.
           BigEndianUnicode  UTF-16 format big-endian byte order.
           BigEndianUTF32    UTF-32 format big-endian byte order.
           UTF8              UTF-8 format.
           UTF7              UTF-7 format.
           String            Use the encoding type for a string. 
           Unknown           Unknown or invalid. The data can be treated as binary.
           Unicode           UTF-16 format little-endian byte order.
           The default encoding is 'Default' the system's active ANSI code page.

   -Stream string
        Add the content to the specified alternate data stream.
        If the stream does not yet, exist, Add-Content creates it.
        Enter the stream name. Wildcards are not supported.
        Stream is a dynamic FileSystem parameter (PowerShell 3.0+)

Under Windows, Add-Content will create ANSI files by default.
In the absence of an explicit -Encoding argument, Add-Content will detect the existing encoding in the destination file and apply it to the new content. If there is no BOM it will create ANSI files by default. Under PowerShell Core edition, the encoding defaults to BOM-less UTF-8. This can be configured via the $PSDefaultParameterValues preference variable.
An alternative is to use Out-File or >> which default to UTF-16LE.

Standard Aliases for Add-Content: ac


Add a UTF8 text string to the end of every .TXT file in the current directory:

PS C:\> add-content -path *.txt -encoding 'UTF8' -value 'This 🍋 is a lemon.'

Add the content of the file Cats.txt to the end of the file Pets.txt:

PS C:\> add-content -path pets.txt -value (get-content c:\docs\cats.txt)

The parentheses above insure Get-Content can complete before the Add-Content starts.
We specify -value to pass the contents of the file (rather than an object.)

Add the current date to a logfile:

PS C:\> add-content -Path MyLogfile.txt -Value (get-date) -passthru

The -passthru option will display the content as it is added, it actually passes an object through the pipeline, which by default is passed back to the command line.

A function to write one line of text to a log file:

function Add-LogFile([string] $line)
   $mainLogFile = "$env:TEMP\ss64Demo-Log.txt"
   Add-Content -Path $ mainLogFile -Value $line

“Content is king” - Webmonkey circa 1996

Related PowerShell Cmdlets

Get-Content - Get content from item (specific location).
Set-Content - Set content in the item (specific location).
Clear-Content - Remove content from a file/item.
Escape characters, Delimiters and Quotes - Add special characters (newlines, tabs etc).
Get-Item - Get a file object or get a registry (or other namespace) object.
get-help - about_namespace.

Copyright © 1999-2024
Some rights reserved