powershellscripts.com

Tutorials  PowerShell Cmdlet Help for Add-Content



NAME
Add-Content

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

SYNTAX
Add-Content [-LiteralPath] [-Value] [-Credential ] [-Exclude ] [-Filt
er ] [-Force] [-Include ] [-PassThru] [-Confirm] [-WhatIf] [-UseTransaction] []

Add-Content [-Path] [-Value] [-Credential ] [-Exclude ] [-Filter ing>] [-Force] [-Include ] [-PassThru] [-Confirm] [-WhatIf] [-UseTransaction] []


DESCRIPTION
The Add-Content cmdlet appends content to a specified item or file. You can specify the content by typing the conte
nt in the command or by specifying an object that contains the content.


PARAMETERS
-Credential
Specifies a user account that has permission to perform this action. The default is the current user.

Type a user name, such as "User01" or "Domain01\User01", or enter a PSCredential object, such as one generated
by the Get-Credential cmdlet. If you type a user name, you will be prompted for a password.

This parameter is not supported by any providers installed with Windows PowerShell.

Required? false
Position? named
Default value
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false

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

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

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

Required? false
Position? named
Default value
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
Accept pipeline input? false
Accept wildcard characters? false

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

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

-LiteralPath
Specifies the path to the items that receive the additional content. Unlike Path, the value of LiteralPath is u
sed 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 tell Windows PowerShell not to interpret any char
acters as escape sequences.

Required? true
Position? 1
Default value
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false

-PassThru []
Returns an object representing the added content. By default, this cmdlet does not generate any output.

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

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

Required? true
Position? 1
Default value
Accept pipeline input? true (ByPropertyName)
Accept wildcard characters? false

-Value
Specifies the content to be added. Type a quoted string, such as "This data is for internal use only", or speci
fy 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 us
e a Get-Content command to get the content and pass it to the Value parameter.

Required? true
Position? 2
Default value
Accept pipeline input? true (ByValue, ByPropertyName)
Accept wildcard characters? false

-Confirm []
Prompts you for confirmation before executing the command.

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

-WhatIf []
Describes what would happen if you executed the command without actually executing the command.

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false

-UseTransaction []
Includes the command in the active transaction. This parameter is valid only when a transaction is in progress.
For more information, see about_Transactions.

Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false


This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer and OutVariable. For more information, type,
"get-help about_commonparameters".

INPUTS
System.Object
You can pipe the objects to be added (the Value) to Add-Content.


OUTPUTS
None or System.String
When you use the Passthru parameter, Add-Content generates a System.String object representing the content. Oth
erwise, this cmdlet does not generate any output.


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 ob
ject. 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 availab
le in your session, type "Get-PsProvider". For more information, see about_Providers.


-------------------------- EXAMPLE 1 --------------------------

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


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





-------------------------- EXAMPLE 2 --------------------------

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


Description
-----------
This command adds the date to the end of the File1.log and File2.log files and then displays the date at the comman
d line. The command uses the Get-Date cmdlet to get the date, and it uses the Value parameter to pass the date to A
dd-Content. The PassThru parameter passes an object representing the added content through the pipeline. Because th
ere 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)


Description
-----------
This command adds the contents of the Weekly.txt file to the end of the Monthly.txt file. It uses the Get-Content c
mdlet 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 beg
ins.

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".





-------------------------- EXAMPLE 4 --------------------------

C:\PS>add-content -value (get-content test.log) -path C:\tests\test134\logs\test134.log


Description
-----------
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 comm
and 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:\T
ests 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 witho
ut the Force parameter.






RELATED LINKS
Online version: http://go.microsoft.com/fwlink/?LinkID=113278
about_Providers
Get-Content
Set-Content
Clear-Content
Get-Item