Home » PowerShell Add-Content

PowerShell Add-Content

by Online Tutorials Library

PowerShell Add-Content | PowerShell Append to file

The Add-Content cmdlet appends the content to the specified item or a file, such as adding words to the file. We can specify the content by typing it in the cmdlet or by specifying an object which contains the content.

Syntax

Parameters

-Path

The -Path parameter is used to specify the path to the items which receives the additional content. Wildcard characters are accepted. The paths which are given in the cmdlet must be paths to the items, not to the containers. If we specify multiple paths, by using commas, we can separate the paths.

-LiteralPath

The -LiteralPath parameter is used to specify a path to one or more locations. Its value is used exactly as it is typed. If the path includes the escape characters, enclose it in single quotation marks. Single quotation mark tells the Windows PowerShell that it should not interpret any character as an escape sequence. There is no character in the cmdlet, which is interpreted as a wildcard.

-WhatIf

The -WhatIf parameter displays ‘what would happen if the cmdlet runs’. The cmdlet will not execute.

-Confirm

The -Confirm parameter prompts a confirmation before running the cmdlet.

-Value

The -Value parameter is used to specify the content to be added. We cannot specify the content of a file by typing its path because a path is just a string. We can use the Get-content cmdlet to get the content and pass it to the -Value parameter.

-PassThru

The -PassThru parameter returns an object which represents the added content. By default, this parameter does not generate any output.

-Filter

The -Filter parameter specifies a filter to qualify the -Path parameter. The FileSystem provider is the only PowerShell provider that supports the uses of filters. This parameter is more efficient as the provider applies the filters when the cmdlet gets the object, rather than having Powershell filters the object after they’re accessed.

-Include

The items that this cmdlet includes in the operation are specified as a string array. The value of -Include parameter qualifies the -Path parameter. Enter a pattern or a path element, such as *.txt. Wildcard characters are accepted. The -Exclude parameter is effective only when the cmdlet includes the contents of an item, such as C:*, the wildcard character ‘*‘ is used to specify the contents of the C: directory.

-Exclude

The items that this cmdlet excludes in operation are specified as a string array. The value of -Exclude parameter qualifies the -Path parameter. Enter a pattern or a path element, such as *.txt. Wildcard characters are accepted. The -Exclude parameter is effective only when the cmdlet includes the contents of an item, such as C:*, the wildcard character ‘*‘ is used to specify the contents of the C: directory.

-Force

The -Force parameter allows to add content to a read-only file and overrides the read-only attributes.

-Encoding

The -Encoding parameter is used to specify the type of encoding for the target file. By default, its value is UTF8NoBOM. It is a dynamic parameter that the FileSystem provider adds to the Add-Content cmdlet. The -Encoding parameter works only in file system drives.

Following are the acceptable values for this parameter:

  • ASCII: This value uses the encoding for the ASCII (7-bit) character set.
  • OEM: This value uses the default encoding for MS-DOS and console programs.
  • UTF8: Encodes in UTF-8 format.
  • UTF8BOM: It encodes in UTF-8 format with Byte Order Mark (BOM)
  • UTF8NoBOM: It encodes in UTF-8 format without Byte Order Mark (BOM)
  • Unicode: It encodes in UTF-16 format using the little-endian byte order.
  • BigEndianUnicode: It encodes in UTF-16 format using the big-endian byte order.
  • UTF32: This value encodes in UTF-32 format.
  • UTF7: This value encodes in UTF-7 format.

-NoNewLine

The -NoNewLine parameter indicates that this cmdlet does not add a new line or carriage return to the content. No newlines or spaces are inserted between the output strings, and there is no newline which is added after the last output string.

-Stream

The -Stream parameter is used to specify an alternate data stream for the content. If the stream does not exist, it creates it. Wildcard characters are accepted.

Examples

Example 1: Add a string to a specified file

PowerShell Add-Content

The cmdlet, in this example, appends a value to a specified file in the current directory.

Example 2: Add a string to all the text files

PowerShell Add-Content

The cmdlet, in this example, appends a value to all the text files in the current directory.

Example 3: Add a date to the end of the specified file

PowerShell Add-Content

The cmdlet in this example appends the date to a specified file in the current directory and shows the date in the PowerShell console.

Example 4: Add content of a specified file to another file

PowerShell Add-Content

This example takes the content from a file and stores it in a variable. And that variable is used to append the content into another file.

The Get-Content cmdlet takes the content of File1.txt and stores it in the $get variable.

The Add-Content cmdlet updates the File2.txt file using the content of the $get variable. The Get-Content cmdlet displays File2.txt.

Example 5: Create a new file and copy the content

PowerShell Add-Content

This example creates a new file and copies the content of an existing file into the new file.


You may also like