Skip to main content

How to Write Text to a File in PowerShell with Set-Content

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Set-Content writes text into a file. It creates the file when it isn't there, and replaces what the file held when it is. This guide writes one line, then several, sends lines through the pipe, and reads the file back with Get-Content, with real output from PowerShell 7.6.10.

The Set-Content page: "Set-Content is a string-processing cmdlet that writes new content or replaces the content in a file."

Syntax​

Syntax
Set-Content -Path <file> -Value <text>
Set-Content -Path <file> -Value <line1>, <line2>
<line1>, <line2> | Set-Content -Path <file>
PartWhat it means
-Path <file>The file to write. Set-Content creates it when it doesn't exist
-Value <text>The text to write, in quotes
<line1>, <line2>Several values, separated by commas: each one becomes a line of the file
| Set-ContentThe text can also come through the pipe instead of -Value

The Example​

The example keeps a shopping list in a file named list.txt, in a folder named set-content-demo. The commands run in the PowerShell 7.6.10 console on a Mac.

Prerequisites​

  • PowerShell 7. This guide uses PowerShell 7.6.10: $PSVersionTable.PSVersion.ToString() printed 7.6.10.

Step 1: Write One Line to a File​

PowerShell
Set-Content -Path list.txt -Value 'milk'
Get-Content -Path list.txt
Output
milk

Set-Content created list.txt, wrote milk into it and printed nothing. The page: "By default, this cmdlet does not generate any output." Get-Content then read the file back. The Get-Content page: "The Get-Content cmdlet gets the content of the item at the location specified by the path, such as the text in a file or the content of a function."

Step 2: Write Several Lines​

PowerShell
Set-Content -Path list.txt -Value 'milk', 'bread', 'eggs'
Get-Content -Path list.txt

macOS-style code window titled PowerShell where Set-Content writes the three values 'milk', 'bread' and 'eggs', separated by commas, to list.txt, and Get-Content reads list.txt back.

Output
milk
bread
eggs

The commas made a list of three values, and Set-Content wrote each one on its own line. The file already held milk from Step 1, and it appears only once: Set-Content replaced the old content instead of adding to it.

Step 3: Send the Lines Through the Pipe​

PowerShell
$fruit = 'plums'
"buy $fruit", 'buy tea' | Set-Content -Path list.txt
Get-Content -Path list.txt
Output
buy plums
buy tea

macOS-style terminal window titled PowerShell where $fruit is set to plums, the values buy $fruit in double quotes and 'buy tea' are piped to Set-Content -Path list.txt, and Get-Content prints buy plums and buy tea.

The two values went through the pipe, |, into Set-Content, which wrote them as two lines. The page: "To send content to Set-Content you can use the Value parameter on the command line or send content through the pipeline." The first value is in double quotes, so PowerShell put the value of $fruit, plums, in place of its name before writing.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting a new line at the end: Set-Content -Path list.txt -Value 'tea' on a file that held milk and breadGet-Content printed only tea: the old lines were goneUse Add-Content -Path list.txt -Value 'tea', which kept milk and bread and added tea after them
Spaces instead of commas: Set-Content -Path list.txt -Value 'milk' 'bread'Set-Content: A positional parameter cannot be found that accepts argument 'bread'.Put a comma between the values
A folder that doesn't exist: Set-Content -Path shop/list.txt -Value 'milk'Set-Content: Could not find a part of the path '/Users/you/set-content-demo/shop/list.txt'.Create the folder first: after New-Item -ItemType Directory -Path shop | Out-Null, the same command worked
An object instead of text: @{ item = 'milk' } | Set-Content -Path list.txtThe file held System.Collections.HashtableSend text. To save an object as it prints, use Out-File, which wrote a Name and Value table

The Set-Content page explains the first row: "Set-Content replaces the existing content and differs from the Add-Content cmdlet that appends content to a file." And the last: "If you pipe non-string objects to Set-Content, it converts the object to a string before writing it. To write objects to files, use Out-File."

FAQ​

Does Set-Content end the file with a line break?​

Yes. After Set-Content -Path list.txt -Value 'milk', Format-Hex -Path list.txt showed the bytes 6D 69 6C 6B 0A: the four letters of milk and a line break, 0A. Add -NoNewline to leave it out: the same command with -NoNewline wrote only 6D 69 6C 6B. The page on -NoNewline: "No newline is added after the last output string."

Which encoding does Set-Content use?​

UTF-8. The page on -Encoding: "Specifies the type of encoding for the target file. The default value is utf8NoBOM." The bytes above start with 6D, the letter m, with no byte order mark in front of it.

Conclusion​

Set-Content -Path list.txt -Value 'milk', 'bread', 'eggs' wrote three lines, and Get-Content read them back. Separate lines with commas or send them through the pipe, remember that each write replaces the file, and use Add-Content when you want to add a line instead.

Additional Resources​

  • Set-Content — every parameter, including Value, NoNewline and Encoding, with examples.
  • Get-Content — reading a file, one line at a time.
  • Add-Content — adding lines to the end of a file.