Click to Rate and Give Feedback
TechNet
TechNet Library
Scripting
Windows PowerShell
 Add-Content
Add-Content

Adds content to the specified items, such as adding words to a file.

Syntax

Add-Content [-path] <string[]> [-passThru] [-value] <Object[]> [-filter <string>]  [-include <string[]>] [-exclude <string[]>] [-force] 
[-credential <PSCredential>] [-whatIf] [-confirm] [<CommonParameters>]

Add-Content [-literalPath] <string[]> [-value] <Object[]> [-include <string[]>] [-exclude <string[]>] [-filter <string>] [-passThru] 
[-force] [-credential <PSCredential>] [-whatIf] [-confirm] [<CommonParameters>]

Detailed Description

The Add-Content cmdlet appends 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.

Parameters

-path <string[]>

Specifies the path to the items that receive the additional content. Wildcards are permitted. If you specify multiple paths, use commas to separate the paths.

Required?

true

Position?

1

Default Value

none

Accept Pipeline Input?

true(ByPropertyName)

Accept Wildcard Characters?

true

-include <string[]>

Adds only the specified items. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as "*.txt". Wildcards are permitted.

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

true

-exclude <string[]>

Omits the specified items. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as "*.txt". Wildcards are permitted.

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

true

-filter <string[]>

Specifies a filter in the provider's format or language. The value of this parameter qualifies the Path parameter. The syntax of the filter, including the use of wildcards, depends on the provider. Filters are more efficient than other parameters, because the provider applies them when retrieving the objects, rather than having Windows PowerShell filter the objects after they are retrieved.

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

true

-value <Object>

Specifies the content to be added. Type a quoted string, such as "This data is for internal use only" 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.

Required?

true

Position?

2

Default Value

none

Accept Pipeline Input?

true(ByValue, ByPropertyName)

Accept Wildcard Characters?

false

-passThru

Passes the object created by this cmdlet through the pipeline. By default, this cmdlet does not pass any objects through the pipeline.

Required?

false

Position?

named

Default Value

False

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-force

Overrides the read-only attribute, allowing you to add content to a read-only file.

Required?

false

Position?

named

Default Value

False

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-credential <PSCredential>

This parameter is not implemented this cmdlet.

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

true(ByPropertyName)

Accept Wildcard Characters?

false

-literalPath <string[]>

Specifies the path to the items that receive the additional content. Unlike Path, the value of LiteralPath is used exactly as it is typed. No characters are interpreted as wildcards. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks instruct Windows PowerShell not to interpret any characters as escape sequences.

Required?

true

Position?

1

Default Value

none

Accept Pipeline Input?

true(ByPropertyName)

Accept Wildcard Characters?

false

-WhatIf

Displays a message that describes the effect of the command, instead of executing the command.

You can use WhatIf alone or with $true or $false values. The $true value (-WhatIf:$true) has the same effect as -WhatIf. The $false value (-WhatIf:$false) suppresses automatic WhatIf behavior that results when the value of the $WhatIfPreference variable is 1.

Required?

False

Position?

Named

Default Value

None

Accept Pipeline Input?

False

Accept Wildcard Characters?

False

-Confirm

Prompts for confirmation before executing the command.

You can use Confirm alone or with $true or $false values. The $true value (-Confirm:$true) has the same effect as -Confirm. The $false value (-Confirm:$false) suppresses automatic confirmation, which occurs when the value of $ConfirmPreference variable is less than or equal to the estimated risk of the cmdlet.

Required?

False

Position?

Named

Default Value

None

Accept Pipeline Input?

False

Accept Wildcard Characters?

False

<CommonParameters>

This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutBuffer and -OutVariable. For more information, see about_CommonParameters.

Input and Return Types

The input type is the type of the objects that you can pipe to the cmdlet. The return type is the type of the objects that the cmdlet emits.

Input Type

Objects. You can pipe a string or other object with content to the Value parameter.

Return Type

None. Unless the Passthru parameter is used. If Passthru is used, an object that represents the stopped job is passed down the pipeline.

Notes

You can also refer to Add-Content by its built-in alias, "ac". For more information, see about_Alias.

EXAMPLES

The following examples show how to use the cmdlet in Windows PowerShell.

Example 1

C:\PS>add-content -path *.txt -exclude help* -value "END"

This command adds "END" to all text files in the current directory, except for those with file names that begin with "help".

Example 2

C:\PS>add-content -Path file1.log, file2.log -Value (get-date) -passthru.

This command adds the date to the end of the File1.log and File2.log files and then displays the date at the command line. The command uses the Get-Date cmdlet to get the date and the Value parameter to pass the date to Add-Content. The Passthru parameter passes an object representing the added content through the pipeline. Because there is no other cmdlet to receive the passed object, it is displayed at the command line.

Example 3

C:\PS>add-content -path monthly.txt -value (get-content c:\rec1\weekly.txt)

This command adds the contents of the Weekly.txt file to the end of the Monthly.txt file. It uses the Get-Content cmdlet to get the contents of the Weekly.txt file and the Value parameter to pass the content of weekly.txt to Add-Content. The parentheses ensure that the Get-Content command is complete before the add-content command begins.

You can also copy the content of Weekly.txt to a variable, such as $w, and then use the Value parameter to pass the variable to Add-Content. In that case, the command would be "add-content -path monthly.txt -value $w".

See Also



Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker