A few days ago, I worked with a client who wanted me to design a Project Management system using SharePoint lists, libraries, and site pages.
The client will receive the project-related files as attachments in their Outlook, which will be saved to the SharePoint document library.
The requirement here was: whenever a new email with an attachment is received, if the file already exists in the SharePoint document library, it should be replaced.
In this step-by-step tutorial, I’ll show you how to quickly set up a flow that automatically replaces files in your SharePoint library using Power Automate.
Replace a File in SharePoint using Power Automate
I have a SharePoint document library named [Project Documents] that consists of various types of documents, including .pdf, .xlsx, .doc, and .ppt, related to projects.

Here, I wanted to replace a file present in the document library that already exists with the email attachment.
Suppose, for example, in the above-mentioned document library, I have a ‘Project Details.xlsx’ File. If I receive an email with ‘Project Details.xlsx’ attachments, then I want to replace the SharePoint file with the attachment received in the email.
You can see the Email Below:

To achieve this, I will create an Automated cloud flow in Power Automate that will be triggered based on the provided trigger.
To do this, see the steps mentioned below:
- On the Power Automate Home page, select Automated cloud flow. If you’re a beginner, check out creating an Automated cloud flow in Power Automate.
- Then, choose to add ‘When a new email arrives (V3)’ trigger. Click on the Advanced parameters drop-down and select the ‘Only with Attachments‘ option.
- Only with Attachments: Select the ‘Yes’ option.

- To get all the files present in the SharePoint document library, add the ‘Get files (properties only)‘ action. Provide Site Address and Library Name.

- Next, I will add a ‘Get Attachment(V2)’ to get the attachment content. Set the parameters below.
- Message Id: Select Message Id from the dynamic content of the trigger.
- Attachment Id: Add Attachment Id here from dynamic content.
Then, it will automatically add a ‘For each’ loop by taking the Attachments value.

By now, Power Automate will retrieve all the files from the SharePoint library and attachments from the Email. Let’s check if the email attachment already exists in SharePoint.
- Following the previous action, add a Condition control to check that the attachment name equals any of the files in the SharePoint library. Set the parameters as given in the table below.
| Choose Value | Operator | Choose Value |
|---|---|---|
| Attachment Name @{outputs(‘Get_Attachment_(V2)’)?[‘body/name’]} | Is equal to | File Name with extension @{items(‘For_each_1’)?[‘{FilenameWithExtension}’]} |
A loop will be added above to retrieve the body/values of the Get Files action.

- Under the True branch, add the ‘Delete file’ action to delete the existing file in the SharePoint document library. Provide the necessary parameters, such as Site Address and File Identifier.
- File Identifier: Select the Identifier from the Get files dynamic content.

- Next, add a ‘Create file’ action to replace the deleted file with an email attachment. Set the required parameters presented below.
- Site Address: Select the specific SharePoint site.
- Folder Path: Choose the document library from the open folder icon.
- File Name: Provide the file name from the email attachment.
- File Content: Choose Content Bytes dynamic content from Get attachment(V2).

The flow is completed now. Save the flow by clicking on the Save icon. The flow will be triggered when a new email arrives in Outlook.

- When the flow executes successfully, you can see the output. The file will be replaced with the attachment email. Refer to the image presented below.


This is how to replace a file in SharePoint using Power Automate.
Have you found this tutorial helpful? Do mention your suggestions in the comment section.
You may also like the following tutorials:
- Replace Value in an Array in Power Automate
- Replace String in JSON using Power Automate
- Replace Apostrophe in String in Power Automate
- Replace Commas With New Lines in Power Automate
- Replace Spaces with Underscores in String using Power Automate

After working for more than 18 years in Microsoft technologies like SharePoint, Microsoft 365, and Power Platform (Power Apps, Power Automate, and Power BI), I thought will share my SharePoint expertise knowledge with the world. Our audiences are from the United States, Canada, the United Kingdom, Australia, New Zealand, etc. For my expertise knowledge and SharePoint tutorials, Microsoft has been awarded a Microsoft SharePoint MVP (12 times). I have also worked in companies like HP, TCS, KPIT, etc.