PowerShell - IF statement case insensitive?

6,923

Isn’t CONTAINS supposed to be case insensitive by default in PowerShell?

The -contains operator is case insensitive.

The .Contains() method is case sensitive.

If you want to use the Contains() method, convert both strings to one case before compare. Something like:

If ($TempArr.ToLower().Contains($TempStr.ToLower()) -ne "True")
Share:
6,923

Related videos on Youtube

SOSidb
Author by

SOSidb

Updated on September 18, 2022

Comments

  • SOSidb
    SOSidb over 1 year

    I am trying to write a PowerShell script that will cycle through the ProxyAddresses for all of my AD user objects. If a user has a SMTP address with [email protected], I want it to check if they also have a matching SMTP address of [email protected] and if not then add it, etc.

    $ADobjects = @(Get-ADObject -Filter 'objectClass -eq "User"' -Properties mailNickname,ProxyAddresses -SearchBase "OU=Test,DC=domain,DC=local")
    
    $TempArr = @()
    $OldDomain = "@domain.local"
    $NewDomain = "@domain.com"
    
    $ADobjects | ForEach-Object { ## Cycle thru each AD object
        $PrimaryProxyAddress = $_.mailNickname+$NewDomain
        $TempStr = ""
        $TempAdd = ""
        If ($ADobjects.Count -ge 1) ## Make sure there is at least one item to work on
            {
            $TempArr = $_.ProxyAddresses ## Set $TempArr so that it contains all of the proxy addresses
            $TempArr | ForEach-Object { ## Cycle thru each proxy address of each AD object
            If ($_.Contains($OldDomain) -eq "True") ## Does the proxy address contain the old domain?
                { ## Come here if the proxy address contains the old domain
                $TempStr = $_ -replace $OldDomain, $NewDomain ## Replace the $OldDomain name with $NewDomain
                If ($TempArr.Contains($TempStr) -ne "True") ## See if we already have an address with the new domain name
                    {
                    write-host $TempStr
                    $TempAdd = $TempAdd + " " + $TempStr ## We don't have one so add it to the list of SMTP addresses to add
                    ## I've removed all of the addition stuff to keep the script shorter
                    }
                }
            }
            }
    }
    

    It works until I get to the part

    If ($TempArr.Contains($TempStr) -ne "True")
    

    The $TempArr array may be something like

    “SMTP:[email protected] smtp:[email protected] smtp:[email protected] smtp:[email protected] x400 etc”
    

    The $TempStr may be something like

    “SMTP:[email protected]

    My $TempStr does exist in the $TempArr array but my IF statement never returns TRUE (so it always thinks that the IF statement is –ne TRUE).

    Isn’t CONTAINS supposed to be case insensitive by default in PowerShell? If it is case insensitive, doesn’t “SMTP:[email protected]” -eq “smtp:[email protected]”? Or is it maybe a data-type issue (I don’t get any errors) since one is an array and the other is a string? What am I missing here?

    Thanks much

  • SOSidb
    SOSidb about 10 years
    Great answer - unfortunately I got the following error: // Method invocation failed because [Microsoft.ActiveDirectory.Management.ADPropertyValueCollect‌​ion] doesn't contain a method named 'ToLower' // But you gave me the info that I needed and I changed the line from a method to an operator and now it works // If ($TempArr -Contains $TempStr -ne "True") // Thank you!!
  • Ƭᴇcʜιᴇ007
    Ƭᴇcʜιᴇ007 about 10 years
    Yeah I was wondering about that. ToLower is for strings, not arrays -- but hey, you figured it out, that's what counts. ;)