How to Replace Spaces with Underscores in String using Power Automate?

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:

  1. 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.
  2. 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.
Power Automate replace spaces with under scores
  1. 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'),' ','_')
Replace spaces with Underscores Power Automate

Save the actions to the flow by clicking on the Save icon. Then, test the flow manually by clicking on the Runflow button.

Output:

Replace spaces with Underscores in String Power Automate

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:

>

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…