Cmdlets

Choose a cmdlet from the list on the left or search for a specific cmdlet. Choose a cmdlet from the list or search for a specific cmdlet.
About Help  Providers  Aliases  Modules
 

Add-Content

Add-Content

Microsoft.PowerShell.Core
Alias: ac

Synopsis

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

Syntax

Add-Content [-Value] [-Confirm] [-Credential] [-Encoding] [-Exclude] [-Filter] [-Force] [-Include] [-LiteralPath] [-NoNewline] [-PassThru] [-Stream] [-UseTransaction] [-WhatIf] [<CommonParameters>]

Add-Content [-Path] [-Value] [-Confirm] [-Credential] [-Encoding] [-Exclude] [-Filter] [-Force] [-Include] [-NoNewline] [-PassThru] [-Stream] [-UseTransaction] [-WhatIf] [<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

-Confirm <SwitchParameter>

Aliases

cf

Required?

false

Position

named

Default value

False

Accept pipeline input?

False

Accept wildcard characters?

false

-Credential <PSCredential>

Aliases

none

Required?

false

Position

named

Default value

None

Accept pipeline input?

True (ByPropertyName)

Accept wildcard characters?

false

-Encoding <FileSystemCmdletProviderEncoding>

Aliases

none

Required?

false

Position

named

Default value

None

Accept pipeline input?

False

Accept wildcard characters?

false

-Exclude <String[]>

Aliases

none

Required?

false

Position

named

Default value

None

Accept pipeline input?

False

Accept wildcard characters?

false

-Filter <String>

Aliases

none

Required?

false

Position

named

Default value

None

Accept pipeline input?

False

Accept wildcard characters?

false

-Force <SwitchParameter>

Aliases

none

Required?

false

Position

named

Default value

False

Accept pipeline input?

False

Accept wildcard characters?

false

-Include <String[]>

Aliases

none

Required?

false

Position

named

Default value

None

Accept pipeline input?

False

Accept wildcard characters?

false

-LiteralPath <String[]>

Aliases

PSPath

Required?

true

Position

named

Default value

None

Accept pipeline input?

True (ByPropertyName)

Accept wildcard characters?

false

-NoNewline <SwitchParameter>

Aliases

none

Required?

false

Position

named

Default value

False

Accept pipeline input?

False

Accept wildcard characters?

false

-PassThru <SwitchParameter>

Aliases

none

Required?

false

Position

named

Default value

False

Accept pipeline input?

False

Accept wildcard characters?

false

-Path <String[]>

Aliases

none

Required?

true

Position

0

Default value

None

Accept pipeline input?

True (ByPropertyName)

Accept wildcard characters?

false

-Stream <String>

Aliases

none

Required?

false

Position

named

Default value

None

Accept pipeline input?

False

Accept wildcard characters?

false

-UseTransaction <SwitchParameter>

Aliases

usetx

Required?

false

Position

named

Default value

False

Accept pipeline input?

False

Accept wildcard characters?

false

-Value <Object[]>

Aliases

none

Required?

true

Position

1

Default value

None

Accept pipeline input?

True (ByPropertyName, ByValue)

Accept wildcard characters?

false

-WhatIf <SwitchParameter>

Aliases

wi

Required?

false

Position

named

Default value

False

Accept pipeline input?

False

Accept wildcard characters?

false

Input Type

System.Object

Return Type

None or System.String

Notes

When you pipe an object to Add-Content *, the object is converted to a string before it is added to the item. The object type determines the string format, but the format might be different than the default display of the object. To control the string format, use the formatting parameters of the sending cmdlet.

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

The Add-Content cmdlet is designed to work with the data exposed by any provider. To list the providers available in your session, type `Get-PsProvider`. For more information, see about_Providers.

*

Examples

Example 1: Add a string to all text files with an exception

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

PS C:\>Add-Content -Path "*.txt" -Exclude "help*" -Value "END"

Example 2: Add a date to the end of the specified files

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 it uses 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.

PS C:\>Add-Content -Path "file1.log", "file2.log" -Value (Get-Date) -PassThru

Example 3: Add the contents of a specified file to another file

This command adds the contents of the Weekly.txt file to the end of the Monthly.txt file. The command uses the Get-Content cmdlet to get the contents of the Weekly.txt file, and it uses 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 $String, and then use the Value parameter to pass the variable to Add-Content .

PS C:\>Add-Content -Path "monthly.txt" -Value (Get-Content "c:\rec1\weekly.txt")

Example 4: Create a new directory and file and copy content

This command creates a new directory and file and copies the content of an existing file to the newly created file.

This command uses the Add-Content cmdlet to add the content. The value of the Value parameter is a Get-Content command that gets content from an existing file, Test.log.

The value of the Path parameter is a path that does not exist when the command runs. In this example, only the C:\Tests directories exist. The command creates the remaining directories and the Test134.log file.

The Force parameter is not required for this command. Add-Content creates directories to complete a path even without the Force parameter.

PS C:\>Add-Content -Value (Get-Content "test.log") -Path "C:\tests\test134\logs\test134.log"

Online Version
Clear-Content
Get-Content
Get-Item
Set-Content