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

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Write to Files with PowerShell Set-Content: A Complete Guide](https://adamtheautomator.com/wp-content/uploads/2019/06/powershell-write-file-set-content-powershell-write-to-file-1.png)

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

[![](https://secure.gravatar.com/avatar/d0b9d42e21e5622713f8b693aa5c0f9244d5f7dd200ed29b8398f52dee5de337?s=192&d=mm&r=g)Adam Bertram](https://adamtheautomator.com/author/adam-bertram/)16 June 20192 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[PowerShell](/tag/powershell/)

Table of Contents

*   [Creating a File/Overwriting a File](#creating-a-file-overwriting-a-file)
*   [Output to a File](#output-to-a-file)

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.

```powershell
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.

```powershell
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.

```powershell
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 -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.

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-write-to-file%2F&text=Write%20to%20Files%20with%20PowerShell%20Set-Content%3A%20A%20Complete%20Guide)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-write-to-file%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-write-to-file%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/06/26995-troubleshoot-dns-issues-powershell-codex.webp)

### [Troubleshoot DNS Issues with PowerShell](/troubleshoot-dns-issues-powershell/)

Troubleshoot DNS issues with PowerShell by testing name resolution, DNS client settings, cache entries, and network connectivity in a repeatable workflow.

![](https://adamtheautomator.com/wp-content/uploads/2025/10/image_2025-10-24_095322075.png)

### [Migrating from PowerShell 6 to 7.5: Breaking Changes/New Features](/migrating-powershell-6-to-7-5/)

Migrate from PowerShell Core 6 to 7.5: breaking changes, features, and testing tips.

![](https://adamtheautomator.com/wp-content/uploads/2025/06/featured-image-6.png)

### [How to Add Timeouts to Pester Tests with PowerShell Runspaces](/pester-test-timeout-runspaces/)

Prevent Pester tests from hanging indefinitely using PowerShell runspaces. Learn to handle variable scoping, module loading, TestDrive access, and stream capture challenges with timeout protection.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
