PowerShell Logo Small

Set-Content



This is the built-in help made by Microsoft for the command 'Set-Content', in PowerShell version 4 - as retrieved from Windows version 'Microsoft Windows 8.1 Enterprise' PowerShell help files on 2016-06-23.

For PowerShell version 3 and up, where you have Update-Help, this command was run just before creating the web pages from the help files.

SYNOPSIS

Replaces the contents of a file with contents that you specify.

SYNTAX


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



Search powershellhelp.space

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.

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

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.



<

RELATED LINKS

Online version: http://technet.microsoft.com/library/hh847827(v=wps.630).aspx
Set-Content
(generic); http://go.microsoft.com/fwlink/?LinkID=113392
FileSystem Provider
Clear-Content
Get-Content
Get-ChildItem
Get-Content
Get-Item
Remove-Item
Set-Content
Test-Path

REMARKS

<

Examples


-------------------------- EXAMPLE 1 --------------------------

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



Description

-----------

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








-------------------------- EXAMPLE 2 --------------------------

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



Description

-----------

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








-------------------------- EXAMPLE 3 --------------------------

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



Description

-----------

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

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.








-------------------------- EXAMPLE 4 --------------------------

C:\PS>Get-Content test.xml | Set-Content final.xml -Force -Encoding UTF8



Description

-----------

This command replaces the contents of the final.xml file with the contents of the test.xml file.

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 e
ncoding of UTF-8.