This blog post explains how to remove unwanted characters from a string in a Power Automate flow. These can be regular characters or unprintable characters, such as control codes.
My example string:
<note> <to>[email protected]</to> <from>Michael Knight</from> <heading>Reminder</heading> <body>Don't forget to clean KITT this weekend!!! </body> </note>
The aim is to remove the apostrophes, exclamation marks and the two non-printable characters. The non-printable characters are visible in Notepad++ :

As you can see from the screenshot, the non-printable characters are the SOH (Start of heading) and BEL (Bell).
It is possible to replace all unwanted characters without any apply to each loops for maximum speed. If you just want implement this flow without understanding how it works, please just skip to the section Quick Paste Scope Code.
Table of contents
Flow Overview
Here is an image of the entire process:

Step by Step Explanation
Here is a step by step explanation of how the flow works.
Create a compose action to define the characters to remove
In the first step, a compose action is defined which contains a JSON array of characters to be removed from the string. If for example you wanted to remove the characters A, B and C, you would define it as:
[ "A", "B", "C" ]
Create a compose action to define the string to have characters removed.
This step is very simple, simply put the string you want to remove characters from in here (you could also use dynamic string content)
Create a Select action to remove any invalid characters from the string.
Most of the work is done within this select action. This is much faster than using an apply to each method as the execution time is near instant. But it does require some explanation. In the from field use the expression:
range(0, length(outputs('String_To_Modify')) )
The range function is useful for all sorts of things, but what we are using it for is to create an array of numbers. Each number represents one character of the string. If the string to have characters removed was:
I love Power Automate!
The range expression would produce the following array:
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21 ]
The array contains an entry to represent each character in the string.
In the map section of the select action the expression used is:
if ( contains(outputs('Unwanted_Characters'), substring(outputs('String_To_Modify'), item(), 1)), '', substring(outputs('String_To_Modify'), item(), 1) )
This if expression checks through each character of the string and checks to see if that character is contained within the array of characters to be removed. If it is not in the array to be removed, then it is simply output as is, if it is found, then an empty string is output instead.
Reconstruct the String
The Select action expects it’s input to be an array, which we have provided using the range expression. It also outputs an array. If I wanted to remove the exclamation marks I would specify the Unwanted Characters compose action as:
[ "!" ]
And the select action would produce the following output:
[ "I", " ", "l", "o", "v", "e", " ", "P", "o", "w", "e", "r", " ", "A", "u", "t", "o", "m", "a", "t", "e", "" ]
Note how the final character which was an exclamation mark has become an empty string.
Reconstruct the string
Now that we have an array of characters with all of the unwanted characters removed. We can rebuild the string with a simply compose action containing the expression:
join(body('RemoveUnwanted'), '')
This will convert the array back into a string which we can then use in further actions within the Flow.
Brief overview of how the flow works:
- A variable called string is defined which contains the complete string including the invalid characters.
- A compose action called RemovalChars is created, which is an array of characters that you’d like to be removed.
- An apply to each loop is created using the array RemovalChars as it’s basis.
- Inside the loop, a compose action called “removeCharacter” uses the replace function to remove the character in the current iteration of the loop.
replace(variables('string'), item(), '')
- The variable string is updated with the output from the compose action.
- Inside the loop, a compose action called “removeCharacter” uses the replace function to remove the character in the current iteration of the loop.
- Finally, the content of the string with the characters removed is placed inside a compose action, so that you can check if the output is as expected.
Non-printable characters
As I mentioned in the start of the post, it is also possible to remove special characters, that are non-printable. In my example I wanted to remove the SOH (Start of heading) and BEL. I represented these as:
[ "u0001", "u0007" ]
These numbers represent the Unicode values of these characters. Which you can lookup on unicode-table.com.
Quick Paste Scope Code
If you want to implement the above in one of your flows, then you can copy and paste the scope above into your flow. Simply do the following:
- Copy the code block below into your clipboard.
- Go into the flow editor and add a new action.
- Click on My Clipboard.
- Press CTRL-V.
- Click on the newly Visible action Remove Unwanted Characters.
- Edit the characters and string as per your requirements.
{ "id": "09aad5c1-1777-4c50-91b6-0698-3fc31b57", "brandColor": "#8C3900", "connectionReferences": {}, "connectorDisplayName": "Control", "icon": "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzIiIGhlaWdodD0iMzIiIHZlcnNpb249IjEuMSIgdmlld0JveD0iMCAwIDMyIDMyIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPg0KIDxwYXRoIGQ9Im0wIDBoMzJ2MzJoLTMyeiIgZmlsbD0iIzhDMzkwMCIvPg0KIDxwYXRoIGQ9Im04IDEwaDE2djEyaC0xNnptMTUgMTF2LTEwaC0xNHYxMHptLTItOHY2aC0xMHYtNnptLTEgNXYtNGgtOHY0eiIgZmlsbD0iI2ZmZiIvPg0KPC9zdmc+DQo=", "isTrigger": false, "operationName": "Remove_Unwanted_Characters", "operationDefinition": { "type": "Scope", "actions": { "Unwanted_Characters": { "type": "Compose", "inputs": ["'", "!", "@", "£", "^", "=", ",", "%", "[", "]", ";", "~", "?", "<", ">", "|", "u0001", "u0007"], "runAfter": {}, "description": "List of unwanted characters to remove (Compose Action)" }, "String_To_Modify": { "type": "Compose", "inputs": "I love Power Automate!", "runAfter": { "Unwanted_Characters": ["Succeeded"] }, "description": "String from which you want to remove characters (Compose Action)" }, "RemoveUnwanted": { "type": "Select", "inputs": { "from": "@range(0, length(outputs('String_To_Modify')) )", "select": "@ifrn(rn contains(outputs('Unwanted_Characters'), substring(outputs('String_To_Modify'), item(), 1)), rn '', rn substring(outputs('String_To_Modify'), item(), 1)rn) " }, "runAfter": { "String_To_Modify": ["Succeeded"] }, "description": "Select action to remove unwanted characters" }, "Re-join_Original_String": { "type": "Compose", "inputs": "@join(body('RemoveUnwanted'), '')", "runAfter": { "RemoveUnwanted": ["Succeeded"] }, "description": "Reconstruct the string" } }, "runAfter": {}, "description": "https://www.tachytelic.net/2020/10/remove-unwanted-characters-power-automate-flow/ " } }
This is a really fast and efficient solution for removing characters from a string in Power Automate. I Hope it helps you out, and if it does, let me know in the comments!
Adam W says
I would like to keep the list of special characters in a SharePoint list so that users can easily update them and then pull the special characters from the list into the flow. How can this be done?
Tristan Holt says
In the “Replace Unwanted Character” compose action, what is ‘NewFileContent’?
Mike M. says
Thank you, Paul. Method 2 is exactly what I needed!
mijo says
Hello, thanks for your job. I have an error on “substring(outputs(‘String_To_Modify’)” because it cannot put array for substring. It needs string. Any ideas ?
Julie Sebby says
It appears the Select action has changed. Map now has an entry for “Key” and “Value”. How would this be updated?
Paulie says
Hi Julie,
Sorry, the blog post does not make it clear. The select action must be put into text mode. As per this image:

Hope this helps.
Paul
BB says
HI and thanks for this great explonation.
I can se if “string to modify” has spaces then it removes them to, how can i prevent it to remove spaces. Ex. Department USA – then my final compose is DepartmentUSA without space.
Paulie says
It should retain the spaces, do you perhaps have a space character in your array of Unwanted Characters?
Amanda Johnson says
Since I can copy/paste the code, but not use it (it doesn’t do anything when I click in clipboard), I am reconstructing the flow manually. However, I’m confused — The scope shows 4 compose actions, however in the “Brief Overview” section you reference a compose action called “RemovalChars” and another called “removeCharacter”, as well as the loop to perform the action. Do you have those documented as well?
J Herschel says
I have the same copy/paste issue, something to do with the RemoveUnwanted step, I think in the select statement. I had to remove that step before copying and pasting…