Set-Content

Write or replace the content in an item. This will overwrite any pre-existing content.

Syntax
      Set-Content [-path] string[]
         [-value] Object[] [-include string[]] [-exclude string[]] 
            [-filter string] [-NoNewline] [-Encoding CharSet]
               [-passThru] [-force] [-Stream string] [-credential PSCredential]
                  [-whatIf] [-confirm] [-UseTransaction] [CommonParameters]

      Set-Content -literalPath string[]
         [-value] Object[] [-include string[]] [-exclude string[]] 
            [-filter string] [-NoNewline] [-Encoding CharSet]
               [-passThru] [-force] [-Stream string] [-credential PSCredential]
                  [-whatIf] [-confirm] [-UseTransaction] [CommonParameters]

      Set-Content [-Stream string] [-Encoding CharSet] [-Force]
         [-Confirm] [-WhatIf] [-UseTransaction] [CommonParameters]

Key
   -Path string
       The path to the item that will receive the content.
       Wildcards are permitted.

   -LiteralPath string
       Like Path above, only the value is used exactly as typed.
       No characters are interpreted as wildcards. If the path includes any
       escape characters then enclose the path in single quotation marks.

   -Encoding CharSet [Dynamic Parameter (FileSystem Only)]
       Encode in a specific character set:
           Unknown           Unknown or invalid. The data can be treated as binary.
           String            Use the encoding type for a string.                
           Unicode           UTF-16 format little-endian byte order.
           Byte              Encode characters as a sequence of bytes.
           BigEndianUnicode  UTF-16 format big-endian byte order.
           UTF8              UTF-8 format.
           UTF7              UTF-7 format.
           UTF32             UTF-32 format.
           UTF8BOM           UTF-8 format with Byte Order Mark (BOM)    * PS 6.0+
           UTF8NOBOM         UTF-8 format without Byte Order Mark (BOM) * PS 6.0+
           ASCII             ASCII (7-bit) character set.
           Default           The system's active ANSI code page   * PS 3.0+
           Oem               Use the default encoding for MS-DOS and console programs, normally ANSI.* PS 3.0+
           BigEndianUTF32    UTF-32 Big Endian  * PS 3.0 to PS 5.1
           The default encoding is 'Default' the system's active ANSI code page.

   -Value Object
       The new content for the item.

   -Exclude string
       Omit the specified items, this qualifies the -Path parameter. 
       Wildcards are permitted. e.g. "*.log"

   -Include string
       Change only the specified items in the Path. 
       Wildcards are permitted. e.g. "*.txt"

   -Filter string
       A filter in the provider's format or language. 
       The exact syntax of the filter (wildcard support etc) depends on the provider.
       Filters are more efficient than -include/-exclude, because the provider
       applies the filter when retrieving the objects, rather than having 
       PowerShell filter the objects after they are retrieved.

   -NoNewline
       Indicates that this cmdlet uses the no newline setting.

   -PassThru 
       Pass the object created by Set-Content through the pipeline.

   -Force
       Override restrictions that prevent the command from succeeding. Force will replace
       the contents of a file, even if the file is read-only, but will not override 
       security permissions. 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 PowerShell 3.0.

   -Credential PSCredential
       Present a user/password credential to validate access to the file.
       This is not yet supported by any providers installed with Windows PowerShell.

   -WhatIf
       Describe what would happen if you executed the command without actually
       executing the command.

   -Confirm
       Prompt for confirmation before executing the command.

   -UseTransaction
       Include the command in the active transaction.

Set-Content is a string-processing cmdlet that writes new content or replaces the content in a file. Set-Content will replace the existing content and differs from the Add-Content cmdlet which will append content to a file.

To send content to Set-Content you can use the -value parameter on the command line or send content through the pipeline.

Under Windows, Set-Content will create ANSI files by default.
This can be configured via the $PSDefaultParameterValues preference variable.

set-content -encoding UTF8 will write a BOM if one is available in the source file, or if the source has been explicitly converted to UTF8 (Get-Content -Encoding UTF8).

Under PowerShell Core edition, the encoding defaults to BOM-less UTF-8.
An alternative is to use Out-File or > which default to UTF-16LE.

Standard Aliases for Set-Content: sc

Examples

Write a string into a text file:

PS C:\> Set-content -path C:\test.txt -value "Hello World"

Write the current date into a CSV file:

PS C:\> get-date | set-content C:\Test_date.csv

Change the format of a file from ASCII to UTF8:

$filename = "c:\docs\demo.csv"
(Get-Content $filename) | Set-Content $filename -Encoding UTF8

Change the format of a file from ASCII (no BOM) to UTF8 (with BOM):

$filename = "c:\docs\demo.csv"
(Get-Content $filename -Encoding UTF8) | Set-Content $filename -Encoding UTF8

Find and Replace some text in every line of the file foo.txt
In this command, the parentheses around (Get-Content) ensure that the Get operation is completed before the Set operation begins, wthout this the two functions would both try to access the file at the same time:

PS C:\> (get-content foo.txt) | `
foreach-object {$_ -replace 'old text', 'new text'} | set-content foo.txt

Find and Replace some text in multiple files (every text file in the current folder):

PS C:\> Get-ChildItem '*.txt' | `
ForEach-Object {(Get-Content $_) -replace 'old text', 'new text' | Set-Content $_.FullName}

Read the content of the file source.txt and save it into destination.txt encoding the file as UTF8:

Get-Content source.txt | Set-Content destination.txt -Encoding UTF8

“A man should hear a little music, read a little poetry, and see a fine picture every day of his life, in order that worldly cares may not obliterate the sense of the beautiful which God has implanted in the human soul” ~ Goethe

Related PowerShell Cmdlets

Add-Content - Add to the content of the item.
Get-Content - Get the content of the item at the specified location.
Clear-Content - Remove content from a file /item.
Copy-Item - Copy an item from one location to another within a namespace.
Set-Item - Change the value of an item.
get-help - about_namespace.
Regular Expressions - Search and Replace within strings.


 
Copyright © 1999-2024 SS64.com
Some rights reserved