PowerShell If-Else String Comparison [With Examples]

In this tutorial, I will explain how to work with string comparison in PowerShell if else. I hope you know how an if-else statement works in PowerShell. Here, we will see a few examples of PowerShell if-else string comparison.

PowerShell If-Else String Comparison

Before showing you the string comparisons, let me give you an overview of the if-else statement PowerShell syntax.

The if-else statement in PowerShell allows you to execute code based on whether a condition is true or false. The basic syntax is as follows:

if (condition) {
    # Code to execute if the condition is true
} else {
    # Code to execute if the condition is false
}

Now, let us check a few examples.

Example-1: Basic String Comparison in if-else statement

In PowerShell, we can do the string comparison in an if-else statement using comparison operators. We can use operators like:-eq (equals) and -ne (not equals).

Here is an example:

Let’s compare two strings, “New York” and “Los Angeles” by using the below if-else statement.

$city1 = "New York"
$city2 = "Los Angeles"

if ($city1 -eq $city2) {
    Write-Output "The cities are the same."
} else {
    Write-Output "The cities are different."
}

In this example, the output will be “The cities are different.”

I executed the above script using VS code, and you can see the output in the screenshot below:

PowerShell if else string comparison

Read PowerShell If Else Statement to Check if a Number is Between Two Values

Example-2: Case-Insensitive String Comparison inside an if-else statement

By default, PowerShell’s comparison operators are case-insensitive. However, you can perform case-sensitive comparisons using -ceq (case-sensitive equals) and -cne (case-sensitive not equals).

Here is an example of how to do case-insensitive string comparisons inside an if-else statement in PowerShell.

$state1 = "California"
$state2 = "california"

if ($state1 -ceq $state2) {
    Write-Output "The states are the same."
} else {
    Write-Output "The states are different."
}

In this case, the output will be “The states are different” because the comparison is case-sensitive.

Check out the output in the screenshot below:

Case-Insensitive String Comparison inside an if-else statement

Check out Case Insensitive Strings Comparison in PowerShell

Example-3: Using Wildcards in String Comparison inside an if-else statement

Inside a PowerShell if-else statement, you can also use wildcard characters for string pattern matching. You can use the -like, -notlike operators.

Here is an example of how to do String Comparison inside an if-else statement using wildcards.

Suppose you want to check if a city name starts with “San”:

$city = "San Francisco"

if ($city -like "San*") {
    Write-Output "The city name starts with 'San'."
} else {
    Write-Output "The city name does not start with 'San'."
}

The output will be “The city name starts with ‘San’.”

Example-4: Use regular expressions for string comparison inside an if-else statement

In PowerShell, you can use regular expressions inside an if-else statement for string comparisons. For this, you can use the -match and -notmatch operators

Let me show you an example.

Let’s check if a ZIP code matches the pattern for ZIP+4 format (e.g., 12345-6789):

$zipCode = "90210-1234"

if ($zipCode -match "^\d{5}-\d{4}$") {
    Write-Output "The ZIP code is in ZIP+4 format."
} else {
    Write-Output "The ZIP code is not in ZIP+4 format."
}

The output will be “The ZIP code is in ZIP+4 format.”

Look at the screenshot below, it is showing the exact required output:

Use regular expressions for string comparison inside an if-else statement PowerShell

I hope you know how to do string comparisons in an if-else statement in PowerShell. In a PowerShell if-else statement, I have shown a few examples related to equality checks, case-sensitive comparisons, wildcard matching, or even regular expression matching for string comparisons.

Feel free to leave a comment if you still have any questions.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.