PowerShell Logo Small

Set-Content



This is the built-in help made by Microsoft for the command 'Set-Content', in PowerShell version 5 - as retrieved from Windows version 'Microsoft Windows Server 2012 R2 Standard' 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

Writes or replaces the content in an item with new content.

SYNTAX


Set-Content [-Path] <String[]> [-Value] <Object[]> [-Credential [<PSCredential>]] [-Encoding {Unknown | String | Unicode | Byte | BigEndianUnicode | UTF8 | UTF7 | UTF32 |
Ascii | Default | Oem | BigEndianUTF32}] [-Exclude [<String[]>]] [-Filter [<String>]] [-Force] [-Include [<String[]>]] [-InformationAction {SilentlyContinue | Stop |
Continue | Inquire | Ignore | Suspend}] [-InformationVariable [<System.Stringal>]] [-PassThru] [-Stream [<System.Stringal>]] [-Confirm] [-WhatIf] [-UseTransaction
[<SwitchParameter>]] [<CommonParameters>]
Set-Content [-Value] <Object[]> [-Credential [<PSCredential>]] [-Encoding {Unknown | String | Unicode | Byte | BigEndianUnicode | UTF8 | UTF7 | UTF32 | Ascii | Default | Oem
| BigEndianUTF32}] [-Exclude [<String[]>]] [-Filter [<String>]] [-Force] [-Include [<String[]>]] [-InformationAction {SilentlyContinue | Stop | Continue | Inquire | Ignore |
Suspend}] [-InformationVariable [<System.Stringal>]] [-PassThru] [-Stream [<System.Stringal>]] -LiteralPath <String[]> [-Confirm] [-WhatIf] [-UseTransaction
[<SwitchParameter>]] [<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.



<

RELATED LINKS

Online Version: http://go.microsoft.com/fwlink/p/?linkid=293909
Add-Content
Clear-Content
Get-Content
about_Providers

REMARKS

<

Examples


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

PS C:\>set-content -path C:\Test1\test*.txt -value "Hello, World"



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 content
by typing it in the command.










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

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



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

PS C:\>(get-content Notice.txt) | foreach-object {$_ -replace "Warning", "Caution"} | set-content Notice.txt



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.