Export (0) Print
Expand All
Collapse the table of content
Expand the table of content
Expand Minimize

Set-Content

Updated: August 9, 2015

Set-Content

Writes or replaces the content in an item with new content.

Aliases

The following abbreviations are aliases for this cmdlet: 

  • sc

Syntax

Parameter Set: Path
Set-Content [-Path] <String[]> [-Value] <Object[]> [-Credential <PSCredential> ] [-Encoding <FileSystemCmdletProviderEncoding> {Unknown | String | Unicode | Byte | BigEndianUnicode | UTF8 | UTF7 | UTF32 | Ascii | Default | Oem | BigEndianUTF32} ] [-Exclude <String[]> ] [-Filter <String> ] [-Force] [-Include <String[]> ] [-NoNewline] [-PassThru] [-Stream <String> ] [-Confirm] [-WhatIf] [-UseTransaction] [ <CommonParameters>]

Parameter Set: LiteralPath
Set-Content [-Value] <Object[]> -LiteralPath <String[]> [-Credential <PSCredential> ] [-Encoding <FileSystemCmdletProviderEncoding> {Unknown | String | Unicode | Byte | BigEndianUnicode | UTF8 | UTF7 | UTF32 | Ascii | Default | Oem | BigEndianUTF32} ] [-Exclude <String[]> ] [-Filter <String> ] [-Force] [-Include <String[]> ] [-NoNewline] [-PassThru] [-Stream <String> ] [-Confirm] [-WhatIf] [-UseTransaction] [ <CommonParameters>]




Detailed Description

The Set-Content cmdlet is a string-processing cmdlet that writes or replaces the content in the specified item, such as a file. Whereas the Add-Content cmdlet appends content to a file, Set-Content replaces the existing content. You can type the content in the command or send content through the pipeline to Set-Content.

Parameters

-Credential<PSCredential>

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.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

true (ByPropertyName)

Accept Wildcard Characters?

false

-Encoding<FileSystemCmdletProviderEncoding>

Specifies the file encoding. The acceptable values for this parameter are:

-- ASCII. Uses the encoding for the ASCII (7-bit) character set.
-- BigEndianUnicode. Encodes in UTF-16 format using the big-endian byte order.
-- Byte. Encodes a set of characters into a sequence of bytes.
-- String. Uses the encoding type for a string.
-- Unicode. Encodes in UTF-16 format using the little-endian byte order.
-- UTF7. Encodes in UTF-7 format.
-- UTF8. Encodes in UTF-8 format.
-- Unknown.: The encoding type is unknown or invalid. The data can be treated as binary.

The default value is ASCII.

Encoding is a dynamic parameter that the FileSystem provider adds to Set-Content. This parameter works only in file system drives.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Exclude<String[]>

Specifies items that this cmdlet omits. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as *.txt. Wildcard characters are permitted.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Filter<String>

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


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Force

Forces the cmdlet to set the contents of a file, even if the file is read-only. Implementation varies from provider to provider. For more information, see about_Providers. Even using the Force parameter, the cmdlet cannot override security restrictions.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Include<String[]>

Specifies items that this cmdlet changes. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as *.txt. Wildcard characters are permitted.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-LiteralPath<String[]>

Specifies the path of the item that receives the content. Unlike Path, the value of the LiteralPath parameter is used exactly as it is typed. No characters are interpreted as wildcard characters. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks tell Windows PowerShell not to interpret any characters as escape sequences.


Aliases

PSPath

Required?

true

Position?

named

Default Value

none

Accept Pipeline Input?

true(ByPropertyName)

Accept Wildcard Characters?

false

-NoNewline

Indicates that this cmdlet uses the no newline setting.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-PassThru

Returns an object that represents the content. By default, this cmdlet does not generate any output.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Path<String[]>

Specifies the path of the item that receives the content. Wildcard characters are permitted.


Aliases

none

Required?

true

Position?

1

Default Value

none

Accept Pipeline Input?

true (ByPropertyName)

Accept Wildcard Characters?

false

-Stream<String>

Specifies an alternative data stream for content. If the stream does not exist, this cmdlet creates it. Wildcard characters are not supported.

Stream is a dynamic parameter that the FileSystem provider adds to Set-Content. This parameter works only in file system drives.

You can use the Set-Content cmdlet to change the content of the Zone.Identifier alternate data stream. However, we do not recommend this as a way to eliminate security checks that block files that are downloaded from the Internet. If you verify that a downloaded file is safe, use the Unblock-File cmdlet.

This parameter was introduced in Windows PowerShell 3.0.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Value<Object[]>

Specifies the new content for the item.


Aliases

none

Required?

true

Position?

2

Default Value

none

Accept Pipeline Input?

true (ByValue, ByPropertyName)

Accept Wildcard Characters?

false

-Confirm

Prompts you for confirmation before running the cmdlet.


Required?

false

Position?

named

Default Value

false

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-WhatIf

Shows what would happen if the cmdlet runs. The cmdlet is not run.


Required?

false

Position?

named

Default Value

false

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

false

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

<CommonParameters>

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

Inputs

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

  • System.Object

    You can pipe an object that contains the new value for the item to this cmdlet.


Outputs

The output type is the type of the objects that the cmdlet emits.

  • None, System.String

    This cmdlet generates a System.String object that represents the content, if you specify the Passthru parameter. Otherwise, this cmdlet does not generate any output.


Notes

  • You can also refer to Set-Content by its built-in alias, sc. For more information, see about_Aliases.

    Set-Content is designed for string processing. If you pipe non-string objects to this cmdlet, it converts the object to a string before writing it. To write objects to files, use Out-File.

    Set-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: Replace the contents of multiple files in a folder

This command replaces the contents of all files in the Test1 folder that have names that start with "test" with "Hello, World". This example shows how to specify content by typing it in the command.


PS C:\> Set-Content -Path "C:\Test1\test*.txt" -Value "Hello, World"

Example 2: Send content to a file

This command creates a comma separated variable length (csv) file that contains only the current date and time. It uses the Get-Date cmdlet to get the current system date and time. The pipeline operator passes the result to Set-Content, which creates the file and writes the content.

If the Test1 directory does not exist, the command fails, but if the file does not exist, the command creates it.


PS C:\> Get-Date | Set-Content -Path "C:\Test1\date.csv"

Example 3: Replace text in a file

This command replaces all instances of Warning with Caution in the Notice.txt file.

It uses Get-Content to get the content of Notice.txt. The pipeline operator sends the results to the ForEach-Object cmdlet, which applies the expression to each line of content in Get-Content. The expression uses the $_ symbol to refer to the current item and the Replace parameter to specify the text to be replaced.

Another pipeline operator sends the changed content to Set-Content which replaces the text in Notice.txt with the new content.

The parentheses around Get-Content make sure that the Get operation is complete before the Set operation starts. Without them, the command fails because the two functions will be trying to access the same file.


PS C:\> (Get-Content -Path "Notice.txt") | ForEach-Object {$_ -Replace "Warning", "Caution"} | Set-Content -Path "Notice.txt"

Related topics

Community Additions

ADD
Show:
© 2016 Microsoft