January 10th, 2016

PowerTip: Use PowerShell to Perform Case-Sensitive Comparison

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:\>

Author

Ed Wilson is the former Microsoft Scripting Guy.

1 comment

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

  • Ronholm, James

    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...

    Read more