PowerShell Function That Compares Two Text Strings (Compare-TextStrings)

Photo of author

By Victor Ashiedu

Published

Do you want to compare two text strings in PowerShell but couldn’t find a Cmdlet to do this? I had the same problem, so I developed a PowerShell script (a function, Compare-TextStrings).

Overview

Use PowerShell Function To Compare Two Text Strings (Compare-TextStrings)

I was writing the article PowerShell IF ELSE Explained when I came across a question about comparing two text strings with PowerShell. To be specific, I want to compare two text strings like the ones below:

"My name is Victor"
"My name is not Tammy"

I want to be able to list the strings that are similar in both text strings. Similarly, I want to list the text strings that are different in both text strings.

Firstly, I tried the Compare-Object cmdlet but as expected, it will not do the job. I played around with logical operators but none of them would do the job.

Finally, I decided to create a custom function that will compare two text strings. I called the function Compare-TextStrings.

Syntaxes

The Compare-TextStrings function has two syntaxes. Here they are…

Compare-TextStrings 
[-FirstTextString] <String[]> 
[-SecondTextString] <String> 
[[-DisplaySimilar]] 
[[-DisplayDifference]] 
[<CommonParameters>]
Compare-TextStrings 
[-FirstTextString] <String[]> 
[-SecondTextString] <String> 
[[-DisplaySimilar]] 
[[-DisplayDifference]] 
[[-OutFile]] [[-Path] <String>] 
[<CommonParameters>]

You use the first syntax to display the result of the command on the PowerShell console. On the contrary, you can use the second syntax to send the output of the command to a text file.

To learn more, read the next section…

Parameters

Use PowerShell Function To Compare Two Text Strings (Compare-TextStrings)

To effectively use the PowerShell function, Compare-TextStrings to compare two text strings, you need to understand the parameters of the function.

In the table below, I have explained all the parameters and how to use them.

Parameters of Compare-TextStringsComments/Notes
FirstTextStringThis parameter is used to specify the first text string you want to compare. It is a required parameter
SecondTextStringUse the SecondTextString to specify the second text string you want to compare in PowerShell. Like the FirstTextString parameter, SecondTextString is a required parameter
DisplaySimilarThe DisplaySimilar parameter is a Switch parameter. It means that it does not require any input. You just need to specify the parameter. So, when you specify the DisplaySimilar parameter, Compare-TextStrings displays the parts of both texts that are similar or the same.
DisplayDifference This is also a Switch parameter. If you specify the DisplayDifference parameter, Compare-TextStrings displays the texts that are different in the texts strings you’re comparing
OutFileYou can use this parameter to send the output of Compare-TextStrings to a text file. The OutFile parameter is a Switch; so it does not require input. The parameter is optional
PathUse the Path parameter to specify the path to save the text file. If the OutFile parameter is specified but the Path parameter is not specified, Compare-TextStrings will save the text file in the user’s temp folder, usually located in [Windows Install Drive]\Users\\AppData\Local\Temp\CompareString. In any case, the name of the textfile is in the format dd-MM-yyyy.txt

Alternatively, if you specify a path with the Path parameter, Compare-TextStrings will save the textfile in $Path + “\CompareString\”

Examples

Use PowerShell To Compare Two Text Strings (Compare-TextStrings): Examples

Now that you know the syntaxes and parameters of Compare-TextStrings, it is time to see some examples.

Before then though, I will show you how to download and install the function.

1. Download and Install

Follow the steps below to download and install the Compare-TextStrings function:

  1. Click the Download Compare-TextStrings link. Then, save the zip file to a folder
  2. Next, unzip the file and save it in:
C:\Program Files\WindowsPowerShell\Modules

To ensure that you get the right path, enter the command below in PowerShell and press enter.

$env:ProgramFiles + "\WindowsPowerShell\Modules"

If this path does not exist, you need to create the full path. To create the path, you can open PowerShell as administrator and run the following commands:

$ModulesFolder = $env:ProgramFiles + "\WindowsPowerShell\Modules"
New-Item -Path $ModulesFolder -ItemType Directory -Force | Out-Null

Once you create the path, copy the unzipped folder, Compare-TextStrings to the last folder in the path…

To give you a visual presentation of what the path looks like, see the screenshot below.

  1. Once you have copied the folder to the Modules folder, open PowerShell as administrator and run the following commands
Import-Module Compare-TextStrings -Force

When you run the command, you’ll be prompted to confirm that you want to install the module. Type R and press enter key.

Now that you have downloaded and installed Compare-TextStrings, proceed to the next sub-sections to see how to use the PowerShell function to compare two text strings.

Before then, though, you can run the command below to display all the examples…

Get-Help Compare-TextStrings -Examples

The command displays all the examples I discuss in the subsequent sub-sections. However, for details, see the sub-sections below this screenshot.

2. Display the Similarity Between Two Text Strings

In this example, I want to display the similarity between the two text strings, “My name is Victor” and “My name is Ashiedu”. Here is the command…

Compare-TextStrings -FirstTextString "My name is Victor" -SecondTextString "My name is Ashiedu" -DisplaySimilar

Here is the result in PowerShell…

How To Display The Similarity Between Two Text Strings In PowerShell Using Compare-TextStrings

3. Display the Difference Between Two Text Strings

In the last example, I compared two text strings and displayed the difference between the text strings. In this example, I want to display the difference between two text strings.

Specifically, I want to display the difference between two text strings, “My name is Victor” and “My name is Ashiedu”. Here is the command…

Compare-TextStrings -FirstTextString "My name is Victor" -SecondTextString "My name is Ashiedu" -DisplayDifference

The difference between this last command and the previous one are the parameters, DisplaySimilar and DisplayDifference.

4. Display the Similarity and the Difference Between Two Text Strings

This example combines the last two examples. This time, I want to display the similarities and differences between two text strings.

Similar to the other examples, my two text strings are: “My name is Victor” and “My name is Ashiedu”. Here is the command:

Compare-TextStrings -FirstTextString "My name is Victor" -SecondTextString "My name is Ashiedu" -DisplaySimilar -DisplayDifference
How To Display The Similarity And The Difference Between Two Text Strings In PowerShell Using Compare-TextStrings

5. Send the Similarity and the Difference Between Two Text Strings to a Text with No Path Parameter

If you rather send the output of the Compare-TextStrings function to a text file, specify the OutFile parameter. Optionally, you can specify the Path parameter.

If you do not specify the Path parameter, the function will save the file in your Temp folder. See the parameters section of this guide for more details.

Here is the command…

Compare-TextStrings -FirstTextString "My name is Victor" -SecondTextString "My name is not Bunmi" -DisplaySimilar -DisplayDifference -OutFile

The command does not return any result on the PowerShell console. Rather, it creates a text file. Here is a screenshot of the text file…

6. Send the Similarity and the Difference Between Two Text Strings to a Text File with the Path Parameter

In the last example, I did not specify the Path parameter. So, the function saved the text file report in $env:TEMP\CompareString.

However, if you want the function to save the text file in a location you specify, use the Path parameter. Here is an example command:

Compare-TextStrings -FirstTextString "My name is Victor" -SecondTextString "My name is not Bunmi" -DisplaySimilar -DisplayDifference -OutFile -Path D:\G-Drive-P

The command saves the text file in D:\G-Drive-P.

How To Send The Similarity And The Difference Between Two Text Strings To A Text File Using Compare-TextStrings (With Path Parameter)

Conclusion

Just like you, I wanted to compare two text strings with PowerShell but I couldn’t find a Cmdlet. So, I created a function called Compare-TextStrings.

You can use this function to compare any two text strings. The function can either display the result in the console or send it to a text file.

In addition to that, you can display the similarity between the text strings. Alternatively, you can display the difference between the two text strings.

I hope you found this custom function useful. I also hope that I was able to explain how the function works.

If so, click on “Yes” beside the “Was this page helpful” question below. You may also express your thoughts and opinions by using the “Leave a Comment” form at the bottom of this page.

Finally, to download more free PowerShell scripts and functions, visit our PowerShell Script Repository page.

We go the extra mile to deliver the highest quality content for our readers. Read our Content Writing, Content Review, and Anti-Plagiarism policies to learn more.

About the Author

Photo of author

Victor Ashiedu

Victor is the founder of InfoPress Media, publishers of Ilifeguides and Itechguides. With 20+ years of experience in IT infrastructure, his expertise spans Windows, Linux, and DevOps. Explore his contributions on Itechguides.com for insightful how-to guides and product reviews.

Related Articles

Get in Touch

If this article does not meet your expectations, kindly let us know. We have various ways you can get in touch with us:

  1. Respond to "Was this page helpful?" above
  2. Leave a comment with the "Leave a Comment" form below
  3. Email us at [email protected] or via the Contact Us page.

Leave a comment