---
title: Write to Files with PowerShell Set-Content: A Complete Guide
description: Discover a complete guide on how to write to files using PowerShell and the Set-Content cmdlet, including tips on overwriting files and appending content.
canonical: https://adamtheautomator.com/powershell-write-to-file/
---

# Write to Files with PowerShell Set-Content: A Complete Guide

> Discover a complete guide on how to write to files using PowerShell and the Set-Content cmdlet, including tips on overwriting files and appending content.

Source: https://adamtheautomator.com/powershell-write-to-file/

---

- Write to Files with PowerShell Set-Content: A Complete Guide Tap to hide Home

- Tutorials

- Guidebooks

- Instructors

- Get Paid to Write

- Advertising

- Recommended Resources

- About Adam

                Search for:

-

-

-

-

# Write to Files with PowerShell Set-Content: A Complete Guide

        Published:16 June 2019 - 2 min. read

- PowerShell

          [](https://adamtheautomator.com/author/adam-bertram/)

            [Adam Bertram](https://adamtheautomator.com/author/adam-bertram/)

            Read [more tutorials](https://adamtheautomator.com/author/adam-bertram/) by Adam Bertram!

-

-

        [](https://specopssoft.com/product/specops-password-auditor/?utm_source=adamtheautomator&utm_medium=referral&utm_campaign=adamtheautomator_referral_na&utm_content=display)
        Audit Active Directory for stale users, weak passwords, and other security risks with [Specops Password Auditor](https://specopssoft.com/product/specops-password-auditor/?utm_source=adamtheautomator&utm_medium=referral&utm_campaign=adamtheautomator_referral_na&utm_content=text).

                XFacebookLinkedIn

Set-Content is one of those core PowerShell cmdlets that I can't do without. I still remember using VBscript before we could use PowerShell to write to a file. I remember always trying to remember what kind of object I needed to use and the method name. Was it FileSystemObject, FileObject or what? It was a pain! Even when I did recall the method name was CreateTextFile(), I'd always forget to add True as the second argument.

Here's an example of the monstrosity I'm talking about.

Set objFSO=CreateObject("Scripting.FileSystemObject")
outFile="c:\file.txt"
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write "test string"
objFile.Close

Compare that VBScript with this PowerShell:

Set-Content -Path 'C:\file.txt' -Value 'test string'

Which one do you prefer? I'll take the PowerShell way! The PowerShell way uses a single cmdlet called Set-content. This cmdlet allows us to much more easily use PowerShell to write to a file. This PowerShell cmdlet is a built-in cmdlet that has one purpose; to write to a file.

It may have some parameters here and there to change up that behavior a bit, but it's solely focused on writing to a file. Not to be confused with Add-Content though since they are similar. Set-Content overwrites the entire file while Add-Content adds to a file. I learned that one the hard way multiple times when I first got started!

## Creating a File/Overwriting a File

The Set-Content cmdlet is fairly simple. Below is a good example. This example shows that Set-Content can both create a file if it doesn't exist or it can replace all of the contents inside of a pre-existing file.

PS> Test-Path -Path C:\test.txt
False
PS> Set-Content -Path C:\test.txt -Value 'foo'
PS> Get-Content -Path C:\test.txt
foo
PS> Set-Content -Path C:\test.txt -Value 'bar'
PS> Get-Content -Path C:\test.txt
bar

## Output to a File

One cmdlet that can output to a file is [Out-File](https://adamtheautomator.com/out-file/), but Set-Content can perform the same task albeit a little differently.

Perhaps you'd like to pull specific pieces of text from one text file and output lines to another file. By using Get-Content and Set-content, you can make this happen. Maybe I've got a text file with one server and their status per line. I'd like to read each server's status and output that to a file. Using Get-Content to read the file and Set-Content to write to file, it's possible.

PS> Get-Content -Path C:\test.txt
SRV1 Status: Up
SRV2 Status: Down
SRV3 Status: Up
PS> Get-Content -Path C:\test.txt | ForEach-Object {
>>> $srvName = $_.Split(' ')[0]
>>> Set-Content -Path "C:\$srvName.txt" -Value $_
}
PS> [Get-ChildItem](https://adamtheautomator.com/get-childitem/) -Filter 'SRV*'
Directory: C:\
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       10/29/2017  12:38 PM             17 SRV1.txt
-a----       10/29/2017  12:38 PM             19 SRv2.txt
-a----       10/29/2017  12:38 PM             17 SRV3.txt

PS> Get-ChildItem -Filter 'SRV*' | Get-Content
SRV1 Status: Up
SRV2 Status: Down
SRV3 Status: Up

Set-Content is one of those core cmdlets that gets used constantly. It's not fancy but works every time and is one you should have in your PowerShell bag of tricks.

For a full breakdown, check out the [Set-Content](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-content?view=powershell-5.1) Microsoft documentation.

  Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

  [Explore ATA Guidebooks](https://adamtheautomator.com/ata-guidebooks/)

## More from ATA Learning and Partners

- ### Recommended Resources! Recommended Resources for Training, Information Security, Automation, and more!

- ### Get Paid to Write! ATA Learning is always seeking instructors of all experience levels. Regardless if you’re a junior admin or system architect, you have something to share. Why not write on a platform with an existing audience and share your knowledge with the world?

- ### ATA Learning Guidebooks ATA Learning is known for its high-quality written tutorials in the form of blog posts. Support ATA Learning with ATA Guidebook PDF eBooks available offline and with no ads!

## Categories

- IT Ops

- Cloud

- DevOps

- Home Ops

- Information Security

- Software Development

## Site

- Home

- Tutorials

- Guidebooks

- Instructors

- Get Paid to Write

- Advertising

- Recommended Resources

- About Adam

        Copyright 2026&copy; ATA Learning | [Privacy Policy](https://adamtheautomator.com/privacy/)

                                                        Don't be left behind with the ATA Learning Newsletter!

Looks like you're offline!
