Set-Content: The Ultimate Guide to Set-Content in PowerShell

“Set-Content” is one of the most useful PowerShell cmdlets that allows you to write or update content to a file. It is a simple yet powerful command that can be used in various scenarios. In this article, we will explore the different ways to use “Set-Content” to write file in Powershell and to make the most out of this cmdlet.

Understanding Set-Content on Windows PowerShell

Set-Content is a PowerShell cmdlet that is used to set the content of a file. It allows you to create or overwrite the content of a file with the specified content. You can use this cmdlet to write strings, arrays, and other objects to a file. Set-Content is a useful tool for automating tasks that involve creating and editing text files.

Parameters of the Set-Content Cmdlet

Like every other cmdlet that can be used in script editors, the Set-Content also has a few parameters. For your easy reference, we made a table of the parameters and their respective functions. You can check it out below – 

PathSpecifies the path of the file in PowerShell to be written to. Can be a single file or a list of files.
LiteralPathSame as Path, but allows for the use of wildcard characters and does not support the escape character.
ValueSpecifies the content to be written to the file(s).
EncodingSpecifies the character encoding of the file(s). 
NoNewLineSuppresses the automatic newline character that is added to the end of each output string.
StreamSpecifies the PowerShell output stream to which the content is written. 
ForceAllows the overwriting of read-only files and files in use by another process. 
WhatIfShows what would happen if the command were run, but does not actually execute the command.
ConfirmPrompts the user for confirmation before executing the command.

How to Write File in PowerShell with Set-Content?

  1. Creating a New File with the Set-Content Cmdlet
  2. Replacing Files’ Contents with the Set-Content
  3. Using Set-Content Filters
  4. Replacing Text in Files

Using the Set-Content Cmdlet on Windows PowerShell

Note: Any mentioned directories and names are used for example purposes only.

Creating a New File with the Set-Content Cmdlet

Set-Content is used to create a new file called file.txt at the specified path. The -Value parameter is used to specify the content of the file. To create a new file using the Set-Content cmdlet in PowerShell, you can use the following command format:

Set-Content -Path "C:\path\to\file.txt" -Value "Content of the file"

The very same procedure can also be used if the file already exists and the contents have to be replaced. There is no difference in procedure for this. To prove this, check the following script line and the projected results – 

Set-Content -Path C:\test.txt -Value 'Chimpanzee'
Get-Content -Path C:\test.txt
Script line for Output Chimpanzee

The result string here would be ‘Chimpanzee’. 

Replacing Files’ Contents with the Set-Content

Get-ChildItem -Path .\Test*.txt | Set-Content -Value "Good Morning"
Get-Content -Path .\Test*.txt
Replacing Files' Contents with the Set-Content

The following PowerShell script demonstrates the use of Get-ChildItem, Set-Content, and Get-Content cmdlets. It first uses the Path parameter of Get-ChildItem to list all .txt files that begin with Test* in the current directory.

Then, the Path parameter of the Set-Content cmdlet is used to specify the Test*.txt files, and the Value parameter provides the text string “Good Morning” that replaces the existing content in each file. 

Finally, the Path parameter of the Get-Content cmdlet is used to specify the Test*.txt files. It also displays each file’s content in the PowerShell console.

Using Set-Content Filters

Set-Content -Path C:\Users\AR\curseforge* -Filter *.txt -Value "Empty"
Using Set-Content Filters

The above-mentioned PowerShell command uses the Set-Content cmdlet to replace the contents of all .txt files in the C:\Users\AR\curseforge directory with the text string “Empty”.

The command specifies the Path parameter as C:\Temp* and the Filter parameter as *.txt to ensure that only .txt files are targeted for modification. The Value parameter specifies the text string to be written into each file.

Replacing Text in Files

One of the other, lesser-known functions of the Set-Content cmdlet is that it can replace text content in files. You can use the example script and result for your reference. 

$oldWord = "old"
$newWord = "new"
$directory = "C:\Path\To\Directory\With\Text\Files"

Get-ChildItem -Path $directory -Filter *.txt | ForEach-Object {
    $fileContent = Get-Content $_.FullName
    $newContent = $fileContent -replace $oldWord, $newWord
    Set-Content $_.FullName $newContent
Replacing Text in Files

In this script, you can specify the directory that contains the .txt files you want to modify by setting the $directory variable. The script uses Get-ChildItem to retrieve all .txt files in the directory and then loops through each file using ForEach-Object.

By now, you should be able to understand what the Set-content cmdlet is and what it can do on your Windows PC through PowerShell. For further queries and FAQs, you can visit the Set-Content page on the Microsoft Learn website.

Meet the Author

Abdul Rahim has been working in Information Technology for over two decades. Learn how Abdul got his start as a Tech Blogger , and why he decided to start this Software blog. If you want to send Abdul a quick message, then visit his contact page here.