When you upload a file with spaces into a SharePoint document library, the file URL will be created, where the spaces are stored as %20.
For example: If there is a file ‘Guidelines for Project Meet Ups’-> the URL will be saved as ‘Guidelines%20for%20Project%20Meet%20Ups.
You can replace the file name with underscores, so that the URL generated is clear and avoids the %20.
In this tutorial, I will explain how to replace a string containing spaces with underscores using Power Automate.
Replace Spaces with Underscore from a String in Power Automate
To explain this, I will take a text string that represents a file name: Stakeholder Meeting Notes 2025 06 25.docx.
I need to replace the spaces with underscores in the string mentioned above, so that the output should be like: Stakeholder_Meeting_Notes_2025_06_25.docx.
To achieve this, you can use the replace() function in Power Automate.
Check out the steps below:
- Create an Instant cloud flow in Power Automate. If you are new to Power Automate, check out how to create a Power Automate Instant cloud flow.
- Then, initialize the text string variable so that the value can be used in the following actions. Add an ‘Initialize variable‘ action. Set the parameters.
- Name: Provide a name for a variable.
- Type: Select the type as ‘String’.
- Value: Take the string value here.

- Next to that, add a ‘Compose‘ action to replace the string having spaces with an underscore. Provide the below expression in the Inputs parameter.
- Inputs: Insert the expression given below.
replace(variables('VarFileName'),' ','_')
Save the actions to the flow by clicking on the Save icon. Then, test the flow manually by clicking on the Runflow button.
Output:

This way, you can replace spaces in a string with an underscore using the Power Automate replace() expression.
I hope you now have an idea about how to replace spaces with underscores in a string using Power Automate. Leave your feedback or suggestions in the comment section!
You may also like the following tutorials:
- Replace String in JSON using Power Automate
- Replace Apostrophe in String in Power Automate
- Replace Commas With New Lines in Power Automate
- Power Automate Replace Special Characters in a String
- Replace Double Quote with Single Quote 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.