---
title: "Text File Management: How to Read and Edit with PowerShell"
description: "Automate text file manipulation. Learn how to read and replace text files effectively with PowerShell."
canonical: "https://adamtheautomator.com/powershell-read-text-file/"
---

# Text File Management: How to Read and Edit with PowerShell

> Automate text file manipulation. Learn how to read and replace text files effectively with PowerShell.

Source: https://adamtheautomator.com/powershell-read-text-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/)

![Text File Management: How to Read and Edit with PowerShell](https://adamtheautomator.com/wp-content/uploads/2019/06/5d238ae5c824b514689ea55d.jpg)

# Text File Management: How to Read and Edit with PowerShell

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

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

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

Table of Contents

*   [Reading the File](#reading-the-file)
*   [Finding and Replacing the String](#finding-and-replacing-the-string)
*   [Writing to the File](#writing-to-the-file)
*   [Dealing with Open File Handles](#dealing-with-open-file-handles)
*   [Resources](#resources)

Need to know how to use PowerShell to read a text file and replace text? This is what we call the PowerShell Read-Text File.

Not a reader? Watch this related video tutorial!

**_Not seeing the video? Make sure your ad blocker is disabled._**

Look no further! This blog post is for you. By the end of the post, I’ll show you a function I built to make your life much easier.

Replacing text in a file with [PowerShel](https://adamtheautomator.com/tag/powershell/)l is three-step process.

1.  Reading the file
2.  Finding and replacing the string
3.  Writing changes to the file.

## Reading the File

You’ll first need to read the file. Let’s first create one with the `Set-Content` cmdlet so we have one to work with.

```powershell
Set-Content -Path 'C:\file.txt' -Value 'foo bar baz'
```

To read this file, you can use the [`Get-Content`](https://adamtheautomator.com/powershell-get-content/ "Get-Content") command. You can read the file by providing a text file path to the `Path` parameter as shown below.

```powershell
$content = Get-Content -Path 'C:\file.txt'
```

## Finding and Replacing the String

Now that we have the file’s content in memory in a string, we need to search and replace the string. One way to do that is to use the `-replace` operator. This PowerShell operator finds a string and replaces it with another.

Using the example file contents, we can provide the search string _foo_ with the replacement string _bar_ which should make the file contents _foo foo baz_ now.

```powershell
PS> $newContent = $content -replace 'foo', 'bar'
bar bar baz
```

## Writing to the File

Now that we have the new file contents saved in `$newContent`, we can now need to write this new content back to the file. One way to do that is to use the `Set-Content` command.

The `Set-Content` command replaces all contents of a file with assigning a new value.

```powershell
$newContent | Set-Content -Path 'C:\file.txt'
```

Whenever you now read the _C:\\file.txt_ file with `Get-Content`, you’ll see that it now contains the new content.

## Dealing with Open File Handles

The steps you previously went through work….most of the time. However, you’ll find in the real world, it doesn’t always turn out that way.

You’ll find that you’ll occasionally have to deal with files that are either open PowerShell itself. This prevents you from writing the new contents back to the file.

To remedy this open file handle situation, I created a simple workflow that allows you to create a temporary text file on disk first with the new contents, remove the original file and then rename the temporary file.

Here’s an example of how it works:

```powershell
$filePath = 'C:\file.txt'
$tempFilePath = "$env:TEMP\$($filePath | Split-Path -Leaf)"
$find = 'foo'
$replace = 'bar'

(Get-Content -Path $filePath) -replace $find, $replace | Add-Content -Path $tempFilePath

Remove-Item -Path $filePath
Move-Item -Path $tempFilePath -Destination $filePath
```

Below is an example of a function I built called `Find-InTextFile` which uses this approach combined with the ability to find (not replace) text in a file.

This function also uses the more powerful regular expression syntax to find strings as well. You’ll find that regular expressions will allow you to more flexible searching using special characters like single quotes, special characters and more.

You can also see below that I’m using a [foreach](https://adamtheautomator.com/powershell-foreach/ "foreach") loop to process multiple files at once. This comes in handy if you have a bunch of files to process.

```powershell
function Find-InTextFile {
    <#
    .SYNOPSIS
        Performs a find (or replace) on a string in a text file or files.
    .EXAMPLE
        PS> Find-InTextFile -FilePath 'C:\MyFile.txt' -Find 'water' -Replace 'wine'
    
        Replaces all instances of the string 'water' into the string 'wine' in
        'C:\MyFile.txt'.
    .EXAMPLE
        PS> Find-InTextFile -FilePath 'C:\MyFile.txt' -Find 'water'
    
        Finds all instances of the string 'water' in the file 'C:\MyFile.txt'.
    .PARAMETER FilePath
        The file path of the text file you'd like to perform a find/replace on.
    .PARAMETER Find
        The string you'd like to replace.
    .PARAMETER Replace
        The string you'd like to replace your 'Find' string with.
    .PARAMETER NewFilePath
        If a new file with the replaced the string needs to be created instead of replacing
        the contents of the existing file use this param to create a new file.
    .PARAMETER Force
        If the NewFilePath param is used using this param will overwrite any file that
        exists in NewFilePath.
    #>
    [CmdletBinding(DefaultParameterSetName = 'NewFile')]
    [OutputType()]
    param (
        [Parameter(Mandatory = $true)]
        [ValidateScript({Test-Path -Path $_ -PathType 'Leaf'})]
        [string[]]$FilePath,
        [Parameter(Mandatory = $true)]
        [string]$Find,
        [Parameter()]
        [string]$Replace,
        [Parameter(ParameterSetName = 'NewFile')]
        [ValidateScript({ Test-Path -Path ($_ | Split-Path -Parent) -PathType 'Container' })]
        [string]$NewFilePath,
        [Parameter(ParameterSetName = 'NewFile')]
        [switch]$Force
    )
    begin {
        $Find = [regex]::Escape($Find)
    }
    process {
        try {
            foreach ($File in $FilePath) {
                if ($Replace) {
                    if ($NewFilePath) {
                        if ((Test-Path -Path $NewFilePath -PathType 'Leaf') -and $Force.IsPresent) {
                            Remove-Item -Path $NewFilePath -Force
                            (Get-Content $File) -replace $Find, $Replace | Add-Content -Path $NewFilePath -Force
                        } elseif ((Test-Path -Path $NewFilePath -PathType 'Leaf') -and !$Force.IsPresent) {
                            Write-Warning "The file at '$NewFilePath' already exists and the -Force param was not used"
                        } else {
                            (Get-Content $File) -replace $Find, $Replace | Add-Content -Path $NewFilePath -Force
                        }
                    } else {
                        (Get-Content $File) -replace $Find, $Replace | Add-Content -Path "$File.tmp" -Force
                        Remove-Item -Path $File
                        Move-Item -Path "$File.tmp" -Destination $File
                    }
                } else {
                    Select-String -Path $File -Pattern $Find
                }
            }
        } catch {
            Write-Error $_.Exception.Message
        }
    }
}
```

## Resources

For more information on the `Set-Content` command check out _[Set-Content: The PowerShell Way to Write to a File](https://adamtheautomator.com/powershell-write-to-file/)_ or an alternative way to write contents to a file, the [`Out-File` command](https://adamtheautomator.com/out-file/).

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-read-text-file%2F&text=Text%20File%20Management%3A%20How%20to%20Read%20and%20Edit%20with%20PowerShell)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-read-text-file%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-read-text-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/)
