Set-Content

Write or replace the content in an item.

Syntax
      Set-Content [[[-path] | [-literalPath] ] string[] ]
         [-value] Object[] [-include string[]] [-exclude string[]] 
            [-filter string] [-Encoding CharSet]
               [-passThru] [-force] [-credential PSCredential]
                  [-whatIf] [-confirm] [-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.

   -Value Object
       The new content for the item.

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

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

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

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

   -Force
       Override restrictions that prevent the command from succeeding, apart
       from security settings. e.g. Force will create file path directories 
       or ignore a files read-only attribute, but will not override security permissions.

   -Credential PSCredential
       Present a user/password credential to validate access to the file.
       This is not yet supported in any Windows PowerShell core commands.

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

   -Confirm
       Prompt for confirmation before executing the command.

   -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.
           ASCII             ASCII (7-bit) character set.

   -UseTransaction
       Include the command in the active transaction.

   CommonParameters:
       -Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
       -OutBuffer -OutVariable.

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

Edit & Replace some text in every line of the file foo.txt :

PS C:\> (get-content foo.txt) | foreach-object {$_ -replace "oldTxt", "newTxt"} | set-content foo.txt

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.

“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:

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.
Set-Item - Change the value of an item
get-help - about_namespace


© Copyright SS64.com 1999-2013
Some rights reserved