Do you wish to evaluate two textual content strings in PowerShell however couldn’t discover a Cmdlet to do that? I had the identical drawback, so I developed a PowerShell script (a operate, Evaluate-TextStrings).
Overview

I used to be writing the article PowerShell IF ELSE Defined after I got here throughout a query about evaluating two textual content strings with PowerShell. To be particular, I wish to evaluate two textual content strings like those under:
"My identify is Victor" "My identify isn't Tammy"
I need to have the ability to checklist the strings which might be related in each textual content strings. Equally, I wish to checklist the textual content strings which might be totally different in each textual content strings.
Firstly, I attempted the Evaluate-Object cmdlet however as anticipated, it is not going to do the job. I performed round with logical operators however none of them would do the job.
Lastly, I made a decision to create a customized operate that may evaluate two textual content strings. I known as the operate Evaluate-TextStrings.
Syntaxes

The Evaluate-TextStrings operate has two syntaxes. Right here they’re…
Evaluate-TextStrings [-FirstTextString] <String[]> [-SecondTextString] <String> [[-DisplaySimilar]] [[-DisplayDifference]] [<CommonParameters>]
Evaluate-TextStrings [-FirstTextString] <String[]> [-SecondTextString] <String> [[-DisplaySimilar]] [[-DisplayDifference]] [[-OutFile]] [[-Path] <String>] [<CommonParameters>]
You utilize the primary syntax to show the results of the command on the PowerShell console. Quite the opposite, you should use the second syntax to ship the output of the command to a textual content file.
To study extra, learn the subsequent part…
Parameters

To successfully use the PowerShell operate, Evaluate-TextStrings to check two textual content strings, it’s worthwhile to perceive the parameters of the operate.
Within the desk under, I’ve defined all of the parameters and the best way to use them.
| Parameters of Evaluate-TextStrings | Feedback/Notes |
|---|---|
| FirstTextString | This parameter is used to specify the primary textual content string you wish to evaluate. It’s a required parameter |
| SecondTextString | Use the SecondTextString to specify the second textual content string you wish to evaluate in PowerShell. Just like the FirstTextString parameter, SecondTextString is a required parameter |
| DisplaySimilar | The DisplaySimilar parameter is a Swap parameter. It signifies that it doesn’t require any enter. You simply must specify the parameter. So, whenever you specify the DisplaySimilar parameter, Evaluate-TextStrings shows the components of each texts which might be related or the identical. |
| DisplayDifference | That is additionally a Swap parameter. In case you specify the DisplayDifference parameter, Evaluate-TextStrings shows the texts which might be totally different within the texts strings you’re evaluating |
| OutFile | You should utilize this parameter to ship the output of Evaluate-TextStrings to a textual content file. The OutFile parameter is a Swap; so it doesn’t require enter. The parameter is elective |
| Path | Use the Path parameter to specify the trail to avoid wasting the textual content file. If the OutFile parameter is specified however the Path parameter isn’t specified, Evaluate-TextStrings will save the textual content file within the person’s temp folder, often situated in [Windows Install Drive]CustomersAppDataLocalTempCompareString. In any case, the identify of the textfile is within the format dd-MM-yyyy.txt
Alternatively, if you happen to specify a path with the Path parameter, Evaluate-TextStrings will save the textfile in $Path + “CompareString” |
Examples

Now that you already know the syntaxes and parameters of Evaluate-TextStrings, it’s time to see some examples.
Earlier than then although, I’ll present you the best way to obtain and set up the operate.
1. Obtain and Set up
Comply with the steps under to obtain and set up the Evaluate-TextStrings operate:
- Click on the Obtain Evaluate-TextStrings hyperlink. Then, save the zip file to a folder
- Subsequent, unzip the file and reserve it in:
C:Program FilesWindowsPowerShellModules
To make sure you get the best path, enter the command under in PowerShell and press enter.
$env:ProgramFiles + "WindowsPowerShellModules"
If this path doesn’t exist, you have to create the complete one. To create the trail, you’ll be able to open PowerShell as administrator and run the next instructions:
$ModulesFolder = $env:ProgramFiles + "WindowsPowerShellModules" New-Merchandise -Path $ModulesFolder -ItemType Listing -Pressure | Out-Null
When you create the trail, copy the unzipped folder, Evaluate-TextStrings to the final folder within the path…
To present you a visible presentation of what the trail seems to be like, see the screenshot under.

- After you have copied the folder to the Modules folder, open PowerShell as administrator and run the next instructions
Import-Module Evaluate-TextStrings -Pressure
While you run the command, you’ll be prompted to verify that you simply wish to set up the module. Sort R and press enter key.

Now that you’ve downloaded and put in Evaluate-TextStrings, proceed to the subsequent sub-sections to see the best way to use the PowerShell operate to check two textual content strings.
Earlier than then, although, you’ll be able to run the command under to show all of the examples…
Get-Assist Evaluate-TextStrings -Examples
The command shows all of the examples I talk about within the subsequent sub-sections. Nonetheless, for particulars, see the sub-sections under this screenshot.

2. Show the Similarity Between Two Textual content Strings
On this instance, I wish to show the similarity between the 2 textual content strings, “My identify is Victor” and “My identify is Ashiedu”. Right here is the command…
Evaluate-TextStrings -FirstTextString "My identify is Victor" -SecondTextString "My identify is Ashiedu" -DisplaySimilar
Right here is the end in PowerShell…

3. Show the Distinction Between Two Textual content Strings
Within the final instance, I in contrast two textual content strings and displayed the distinction between the textual content strings. On this instance, I wish to show the distinction between two textual content strings.
Particularly, I wish to show the distinction between two textual content strings, “My identify is Victor” and “My identify is Ashiedu”. Right here is the command…
Evaluate-TextStrings -FirstTextString "My identify is Victor" -SecondTextString "My identify is Ashiedu" -DisplayDifference
The distinction between this final command and the earlier one are the parameters, DisplaySimilar and DisplayDifference.

4. Show the Similarity and the Distinction Between Two Textual content Strings
This instance combines the final two examples. This time, I wish to show the similarities and variations between two textual content strings.
Much like the opposite examples, my two textual content strings are: “My identify is Victor” and “My identify is Ashiedu”. Right here is the command:
Evaluate-TextStrings -FirstTextString "My identify is Victor" -SecondTextString "My identify is Ashiedu" -DisplaySimilar -DisplayDifference

5. Ship the Similarity and the Distinction Between Two Textual content Strings to a Textual content with No Path Parameter
In case you relatively ship the output of the Evaluate-TextStrings operate to a textual content file, specify the OutFile parameter. Optionally, you’ll be able to specify the Path parameter.
If you don’t specify the Path parameter, the operate will save the file in your Temp folder. See the parameters part of this information for extra particulars.
Right here is the command…
Evaluate-TextStrings -FirstTextString "My identify is Victor" -SecondTextString "My identify isn't Bunmi" -DisplaySimilar -DisplayDifference -OutFile
The command doesn’t return any outcome on the PowerShell console. Relatively, it creates a textual content file. Here’s a screenshot of the textual content file…

6. Ship the Similarity and the Distinction Between Two Textual content Strings to a Textual content File with the Path Parameter
Within the final instance, I didn’t specify the Path parameter. So, the operate saved the textual content file report in $env:TEMPCompareString.
Nonetheless, if you need the operate to avoid wasting the textual content file in a location you specify, use the Path parameter. Right here is an instance command:
Evaluate-TextStrings -FirstTextString "My identify is Victor" -SecondTextString "My identify isn't Bunmi" -DisplaySimilar -DisplayDifference -OutFile -Path D:G-Drive-P
The command saves the textual content file in D:G-Drive-P.

Conclusion
Identical to you, I needed to check two textual content strings with PowerShell however I couldn’t discover a Cmdlet. So, I created a operate known as Evaluate-TextStrings.
You should utilize this operate to check any two textual content strings. The operate can both show the outcome within the console or ship it to a textual content file.
Along with that, you’ll be able to show the similarity between the textual content strings. Alternatively, you’ll be able to show the distinction between the 2 textual content strings.
I hope you discovered this tradition operate helpful. I additionally hope that I used to be in a position to clarify how the operate works.
If that’s the case, click on on “Sure” beside the “Was this web page useful” query under. You might also categorical your ideas and opinions by utilizing the “Depart a Remark” type on the backside of this web page.
Lastly, to obtain extra free PowerShell scripts and features, go to our PowerShell Script Repository web page.
