Skip to main content
PDQ.com mobilePDQ.com desktop
Support

Set-Content

Replaces the contents of a file with contents that you specify.
Set-Content [-Value*] <Object[]> [-Confirm] [-Credential <PSCredential>] [-Encoding {Unknown | String | Unicode |Byte | BigEndianUnicode | UTF8 | UTF7 | UTF32 | Ascii | Default | Oem | BigEndianUTF32}] [-Exclude <String[]>][-Filter <String>] [-Force] [-Include <String[]>] -LiteralPath* <String[]> [-NoNewline] [-PassThru] [-Stream<String>] [-UseTransaction] [-WhatIf] [<CommonParameters>]
Set-Content [-Path*] <String[]> [-Value*] <Object[]> [-Confirm] [-Credential <PSCredential>] [-Encoding {Unknown |String | Unicode | Byte | BigEndianUnicode | UTF8 | UTF7 | UTF32 | Ascii | Default | Oem | BigEndianUTF32}][-Exclude <String[]>] [-Filter <String>] [-Force] [-Include <String[]>] [-NoNewline] [-PassThru] [-Stream<String>] [-UseTransaction] [-WhatIf] [<CommonParameters>]
Set-Content [-Encoding {Unknown | String | Unicode | Byte | BigEndianUnicode | UTF8 | UTF7 | UTF32 | Ascii |Default | Oem}] [-Force] [-Stream <string>] [-Confirm] [-WhatIf] [-UseTransaction] [<CommonParameters>]

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 .In file system drives, the Set-Content cmdlet overwrites or replaces the content of one or more files with the content that you specify. This cmdlet is not valid on folders.

Note: This custom cmdlet help file explains how the Set-Content cmdlet works in a file system drive. For information about the Set-Content cmdlet in all drives, type "Get-Help Set-Content -Path $null" or see Set-Content at http://go.microsoft.com/fwlink/?LinkID=113392.

Parameters

-Encoding <FileSystemCmdletProviderEncoding>

  • Default value is ASCII

Specifies the file encoding. The default is ASCII.

Valid values 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.

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

-Force [<SwitchParameter>]

Replaces the contents of a file, even if the file is read-only. Without this parameter, read-only files are not changed.

-Stream <string>

Creates or replaces the content in the specified alternate data stream. If the stream does not yet exist, Set-Content creates it. Enter the stream name. Wildcards are not supported.

Stream is a dynamic parameter that the FileSystem provider adds to the Set-Content cmdlet. 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, it is not the recommended 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 is introduced in Windows PowerShell 3.0.

-Confirm [<SwitchParameter>]

Prompts you for confirmation before executing the command.

-WhatIf [<SwitchParameter>]

Describes what would happen if you executed the command without actually executing the command.

-UseTransaction [<SwitchParameter>]

Includes the command in the active transaction. This parameter is valid only when a transaction is in progress.

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

-Exclude <String[]>

  • Default value is None
  • Accepts pipeline input False

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.

-Filter <String>

  • Default value is None
  • Accepts pipeline input False

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.

-Include <String[]>

  • Default value is None
  • Accepts pipeline input False

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.

-LiteralPath <String[]>

  • This value is required
  • Default value is None
  • Accepts pipeline input ByPropertyName

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.

-NoNewline [<SwitchParameter>]

  • Default value is False
  • Accepts pipeline input False

Indicates that this cmdlet uses the no newline setting.

-PassThru [<SwitchParameter>]

  • Default value is False
  • Accepts pipeline input False

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

-Path <String[]>

  • This value is required
  • Default value is None
  • Accepts pipeline input ByPropertyName

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

-Value <Object[]>

  • This value is required
  • Default value is None
  • Accepts pipeline input ByPropertyName

Specifies the new content for the item.

<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,ErrorAction, ErrorVariable, WarningAction, WarningVariable,OutBuffer, PipelineVariable, and OutVariable.

Inputs
System.Object[], System.String[], System.Management.Automation.PSCredential
You can pipe a value (object), a path, or a credential object to Set-Content
Outputs
None or System.String
When you use the Passthru parameter, Set-Content generates a System.String object representing the content. Otherwise, this cmdlet does not generate any output.
Examples
  1. This command replaces the contents of all files in the Test1 directory that have names beginning with "test" with "Hello, World":
    C:\PS> Set-Content -Path C:\Test1\test*.txt -Value "Hello, World"
    

    This example shows how to specify content by typing it in the command.

  2. This command creates a comma-separated variable-length (csv) file that contains only the current date and time:
    C:\PS> Get-Date | Set-Content C:\Test1\date.csv
    

    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 will create it.

  3. This command replaces all instances of "Warning" with "Caution" in the Notice.txt file:
    C:\PS> Get-Content Notice.txt | ForEach-Object {$_ -replace "Warning", "Caution"} | Set-Content Notice.txt
    

    It uses the Get-Content cmdlet 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 the Get-Content command ensure that the Get operation is complete before the Set operation begins. Without them, the command will fail because the two functions will be trying to access the same file.

  4. This command replaces the contents of the final.xml file with the contents of the test.xml file:
    C:\PS> Get-Content test.xml | Set-Content final.xml -Force -Encoding UTF8
    

    The command uses the Force parameter so that the command is successful even if the Final.xml file is read-only. It uses the Encoding dynamic parameter to specify an encoding of UTF-8.

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

 *
Back to PowerShell Command list
PDQ.com
© 2020 PDQ.com Corporation

Products

  • PDQ Deploy
  • PDQ Inventory
  • PDQ Link
  • Enterprise SL
  • Pricing
  • Downloads
  • Licensing
  • Buy