PowerShell Logo Small

Set-Content



This is the built-in help made by Microsoft for the command 'Set-Content', in PowerShell version 2 - as retrieved from Windows version 'Microsoft® Windows Vista™ Ultimate ' 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 [-LiteralPath] <string[]> [-Value] <Object[]> [-Credential <PSCredential>] [-Exclude <string[]>] [-Filter <string>] [-Force] [-Includ
e <string[]>] [-PassThru] [-Confirm] [-WhatIf] [-UseTransaction] [<CommonParameters>]
Set-Content [-Path] <string[]> [-Value] <Object[]> [-Credential <PSCredential>] [-Exclude <string[]>] [-Filter <string>] [-Force] [-Include <stri
ng[]>] [-PassThru] [-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-C
ontent cmdlet appends content to a file, Set-Content replaces the existing content. You can type the content in the command or send content throu
gh the pipeline to Set-Content.



<

RELATED LINKS

Online version: http://go.microsoft.com/fwlink/?LinkID=113392
about_Providers
Add-Content
Get-Content
Clear-Content

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 sh
ows how to specify content 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 ge
t 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 appl
ies the expression to each line of content in Get-Content. The expression uses the "$_" symbol to refer to the current item and the Replace param
eter 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 comma
nd will fail because the two functions will be trying to access the same file.