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.
Syntax
Add-Content [ [-path] | -literalPath ] string[] [-value] Object[]
[-include string[]] [-exclude string[]]
[-filter string] [-Encoding CharSet]
[-passThru] [-force] [-credential PSCredential]
[-confirm] [-whatIf] [-UseTransaction] [CommonParameters]
Key
-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. 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.
-PassThru
Pass the object created by this cmdlet through the pipeline.
-Force
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 or create directories to complete a
file path, but it will not attempt to change file permissions.
-Credential PSCredential
Present a user/password credential to validate access to the file.
This is not yet supported in any Windows PowerShell core commands.
-Confirm
Prompt for confirmation before executing the command.
-WhatIf
Describe what would happen if you executed the command without actually executing it.
-UseTransaction
Include the command in the active transaction.
-Encoding CharSet [Dynamic Parameter (FileSystem Only)]
Encode in a specific character set:
Unknown Unknown or invalid. The data can be treated as binary.
String Use the encoding type for a string.
Unicode UTF-16 format little-endian byte order.
Byte Encode characters as a sequence of bytes.
BigEndianUnicode UTF-16 format big-endian byte order.
UTF8 UTF-8 format.
UTF7 UTF-7 format.
ASCII ASCII (7-bit) character set.
CommonParameters:
-Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
-OutBuffer -OutVariable.
Examples
Add a text string to the end of every .TXT file in the current directory:
C:\PS> add-content -path *.txt -value "This is the end"
Add the content of the file Cats.txt to the end of the file Pets.txt:
C:\PS> 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:
C:\PS> 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.
"Content is king" - Webmonkey @Wired.com circa 1996
Related:
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