Write or replace the content in an item.

      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]

   -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. (The Default)
           Default           Use the default output normally ASCII   * 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

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

       Indicates that this cmdlet uses the no newline setting.

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

       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.

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

       Prompt for confirmation before executing the command.

       Include the command in the active transaction.

Under Windows, Set-Content will create ANSI files by default.
This can be configured via the $PSDefaultParameterValues preference variable.
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


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

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 "oldTxt", "newTxt"} | set-content foo.txt

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.

Copyright © 1999-2021
Some rights reserved