Efficient Bulk Text Conversion with PowerShell

Are you dealing with text data that you need to convert different characters or words or symbols in bulk? In this blog post, I’ll introduce a powershell script that streamlines the conversion process, making it easy to work with text data across different languages and character sets.

Note: I wrote a post about powershell template text generator a few months ago which is about generating multiple text files or text contents from source template by powershell. Here today post is about text replacement in the existing text file. Use any of these methods to suit your requirements.

The script accepts a csv or txt file as source file, make a replacement of the string or chracters according to the mapping rule given as a parameter -CharacterMappingString. The following mapping string will convert  with A and à with a and so on.

‘Â:A,à:a,É:E,è:e’

In the above string, full colon (:) is used to separate the pair of character as which can be defined in -PairDelimiter parameter and the comma (,) used to separate character-pairs from the string which can be defined in -StringDelimiter parameter.


Parameter Description:

FilePath – The input file path for any readable txt or csv file.

OutputFilePath – The output file path.

ColumnsToSkip – Column name on which the character conversion will be skipped.

InputEncoding – Encoding used in reading the source file.

OutputEncoding – Encoding used to write the destination file.

WordMatchOnly – If used, the change will happen if the characters match only the entire word. Not applicable to special characters.

CharacterMapping – The character mapping on which characters replacement or string replacement will happen based on these mapping pairs. Note that it’s case-sensitive replacement. The entire string should be single quoted. Each Pair of strings or characters is comma-delimited (by default) and each mapping is fullcolon-delimited (by default).
Eg: ‘Ä:A,â:a’
If there is any single quote in the mapping pair, then you must write this single quote as two single quotes.
Eg: ‘Ä:A,â:a,”:SingleQuote,”:DoubleQuotes’

StringDelimiter – The delimiter used to split each pair out of the string. Eg., in the below line, the comma (,) is used to delimit the string by default.
‘Ä:A,â:a,1st:First,2nd:Second’
But, if the mapping string contains comma (,) in one of the mapping pair, then you must use another delimiter in -StringDelimiter parameter which is not included in the mapping string. Example is pipe (|) and the command parameters will look like this:

-CharacterMapping ‘,:Comma|Ä:A|â:a|1st:First|2nd:Second’ -StringDelimiter ‘|’

PairDelimiter – The delimiter used to split each mapping pair. Eg., in the below line, fullcolumn is used to split the mapping pair by default.
‘Ä:A|â:a|1st:First|2nd:Second’
But, if the mapping pair contains fullcolon in one of the mapping pair, then you must use another delimiter which is not included in the mapping pair. Example is using semicolon (;) and the command parameters will look like this:

-CharacterMapping ‘:;FullColon|Ä;A|à;a|â;a|1st;First|2nd;Second’ -StringDelimiter ‘|’ -PairDelimiter ‘;’


Let’s take an example of converting some Spanish characters to English directly. We also convert 1st, 2nd and 3rd to First, Second and Third respectively in the same script. Below is the command.

.\Replace_Character_Pairs_v1.1.ps1 -FilePath .\SourceFile.csv -OutputFilePath .\destinationFile.csv -CharacterMappingString '1st:First,2nd:Second,3rd:Third,ó:o,í:i' -ColumnsToSkip Secret

As you can see, it features a -ColumnsToSkip parameter, allowing users to specify which columns to skip during the conversion process. See the example screenshot below.

Example Text Conversion
Example: Convert characters of a csv file

Let’s take another example with txt file. Here we will change text file contents of a cisco switch commands.

.\Replace_Character_Pairs_v1.1.ps1 -FilePath .\SourceFile.txt -OutputFilePath .\destinationFile.txt -CharacterMappingString '255.255.255.0:255.255.128.0|description Connection:description Site3_Connection' -ColumnsToSkip -StringDelimiter '|' -PairDelimiter ':'

In the above command line, according to the mapping string, we will change the subnet mask of 255.255.255.0 to 255.255.128.0 and append Site3_ to Connection. The result will look like in the following screenshot.

Example: Convert text file contents

Here is another example of converting special characters. It supports all special characters except single quote (‘) in which you need to write two single quotes instead of one in the mapping string. Check the screenshot below.

.\Replace_Character_Pairs_v1.1.ps1 -FilePath .\SourceFile.txt -OutputFilePath .\destinationFile.txt -CharacterMappingString ''';Single Quote|";Double Quote|$;Dollar Sign|&;Ampersand|#;Hash Sign|:;Full Colon|,;Comma' -StringDelimiter '|' -PairDelimiter ';'
Example: Converting special characters

Download the script from github.

Leave a Reply

Your email address will not be published. Required fields are marked *