PowerTip: Use PowerShell to Perform Case-Sensitive Comparison

mredwilson

Summary: Learn how to perform a case-sensitive comparison in Windows PowerShell.

Hey, Scripting Guy! Question I need to compare two strings while taking case sensitivity into account. I try using -eq, but it does not work.
            How can I use Windows PowerShell to perform a case-sensitive comparison?

Hey, Scripting Guy! Answer Use the -ceq operator instead of -eq. Here are two examples that compare the results of -eq and -ceq:

PS C:\> 'scripting guys' -eq 'Scripting Guys'

True

PS C:\> 'scripting guys' -ceq 'Scripting Guys'

False

PS C:\>

1 comment

Discussion is closed. Login to edit/delete existing comments.

  • Ronholm, James 0

    Hi Scripting Guys

    I’ve programmed quite a bit in other languages but I’m fairly new to PowerShell. I’m aware of the case sensitive versions of the comparison operators but I’m nervous about some results..

    I assumed that PowerShell used ASCII (Unicode) values when doing string comparisons so I expected
    ‘abc’ -clt ‘ABC’
    to result in False – but it didn’t

    This makes some intuitive sense (lower must be smaller than upper – right?) but it doesn’t make sense from an ASCII point of view. Has this just been reversed for the Roman alphabet characters – or are there more surprises waiting for me?

    Ex
    “#$%” -clt “)*+”
    works as expected.

Feedback usabilla icon