Power Automate Replace Text in File

One of my colleagues asked me to assist with replacing text in a text file stored in a SharePoint document library, so that he can send an email with the updated document file to the client.

We can achieve this by using the replace() function in Power Automate.

In this tutorial, I will explain how you can replace text in a file present in a SharePoint document library using Power Automate.

Check how you can do!

Replace Text in SharePoint File by Power Automate

Let’s consider that I have a SharePoint document library that consists of various file types, such as .docx, .txt, .ppt, and .xlsx.

You can see the SharePoint library in the image below: Invoice Documents

Replace a Text String in file using Power Automate

Here, my requirement is to replace the string present in the text file with another text.

Suppose the text file consists of Customer Invoice Details, where I wanted to replace ‘Invoice Number’ with ‘Invoice ID’.

power automate replace text in file

To achieve this, I will create a manual trigger flow in Power Automate that a button click can trigger.

To achieve this, follow the steps mentioned below:

  1. On Power Automate, create an ‘Instant cloud flow’. If you’re not familiar with creating one, check out Instant Cloud Flow in Power Automate.
  2. Next, add ‘Get file content‘ to retrieve the content from the text file. Provide the below-mentioned parameters.
    • Site Address: Provide the SharePoint site address.
    • File Identifier: Select the file from the open folder icon.
Replace Text in SharePoint Text File by Power Automate
  1. Then, take a ‘Compose‘ action to provide the replace expression that replaces the text from the file.
    • Inputs: Enter the expression below.
replace(outputs('Get_file_content')?['body'],'Invoice Number','Invoice ID')

From the expression, to get the body of a file from the outputs of Get file content, you need to use the [‘body‘] in the expression.

power automate replace text in file
  1. To view the output, I will send an email with the replaced text file. So, I will add ‘Send an email(V2 )‘ action: set To, Subject, and Body parameters.
    • Attachments: Click on the switch details to input the array icon. Paste the below code.
[
  {
    "Name": "Customer Invoice Details.txt",
    "ContentBytes": @{outputs('Compose_-_Replace_Text')}
  }
]

Replace the Outputs of compose with your compose outputs in the above expression.

Power Automate Replace Text in a File SharePoint
  1. The flow is ready now. Save the flow to the canvas. Then, test it by manually clicking the Runflow button.
  2. When the flow executes successfully, an email will be sent with a text file attachment, where you can see that the text “[Invoice Number]” has been replaced with another text string, “[Invoice ID]”.

    Outputs: Reference the image below.

    Power Automate Replace Text in a SharePoint file
    Power Automate Replace Text in a file

    This is how you can replace a text in a text file using the replace() function in Power Automate.

    Hope you found this article informative. Do mention your suggestions on this tutorial in the comments section.

    You can check out other Power Automate Tutorials:

    >

    Build a High-Performance Project Management Site in SharePoint Online

    User registration Power Apps canvas app

    DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

    Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App

    Power Platform Tutorial FREE PDF Download

    FREE Power Platform Tutorial PDF

    Download 135 Pages FREE PDF on Microsoft Power Platform Tutorial. Learn Now…