Compare the Contents of Two String Objects in PowerShell

Rohan Timalsina Jan 30, 2023 Jan 23, 2022
  1. Use the -eq Operator to Compare the Contents of Two String Objects in PowerShell
  2. Using the -like Operator to Compare the Contents of Two String Objects in PowerShell
  3. Using the .Equals() Method to Compare the Contents of Two String Objects in PowerShell
Compare the Contents of Two String Objects in PowerShell

A String is one of the most common data types used in PowerShell. It represents the sequence of characters or texts. You can define a string in PowerShell by using single or double-quotes.

The PowerShell String is always an object with a System.String type. This tutorial will teach you to compare the contents of two string objects in PowerShell.

Here is an example of a string object.

$data = "Learn Programming"
$data

Output:

Learn Programming

You can check the data type using the GetType() method.

$data.GetType()

Output:

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

Use the -eq Operator to Compare the Contents of Two String Objects in PowerShell

The comparison operators in PowerShell allow you to compare values that match specified patterns. The equality operator -eq checks for the equality of two values. The -eq operator lets you compare the contents of two string objects in PowerShell. It returns True when both values match; otherwise, it returns False.

$a="powershell string"; $b="powershell compare string"; $c= "powershell string"

Comparing the contents of $a and $c:

$a -eq $c

Output:

True

Comparing the contents of $a and $b:

$a -eq $b

Output:

False

The eq operator is case-insensitive. You can use the -ceq operator for case-sensitive equality.

$a -ceq "PowerShell String"

Output:

False

Using the -like Operator to Compare the Contents of Two String Objects in PowerShell

The matching operator -like finds elements that match or do not match a specified pattern. The -like operator also allows you to compare the contents of two string objects in PowerShell and returns a Boolean value, True or False.

Comparing the contents of $a and $c:

$a -like $c

Output:

True

Comparing the contents of $a and $b:

$a -like $b

Output:

False

Using the .Equals() Method to Compare the Contents of Two String Objects in PowerShell

The .Equals() method determines whether the values in two objects are equal or not. You can compare the contents of two string objects in PowerShell with the .Equals() method. It also returns a Boolean value: True when equal or False when not equal.

Comparing the contents of $a and $c:

$a.Equals($c)

Output:

True

Comparing the contents of $a and $b:

$a.Equals($b)

Output:

False
Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

Related Article - PowerShell String