none
Array Contains String not comparing.

    Question

  • Having issues trying to see if an array contains a value doesn't seem to be doing the compare.

    My Code

    $excludedUsers = @("user1","user2")
    $excludedUsers.Contains("user1")
    $users = Get-WmiObject -Class Win32_UserAccount -Filter  "LocalAccount='True'" | select Name,PasswordExpires
    foreach ($user in $users){
        $passwordExpire = $user.PasswordExpires
        $userName = $user.Name
        Write-Host "User:$userName - PasswordExpires:$PasswordExpire"
        If($excludedUsers.Contains($userName)){
            write-host "Excluded"
        }
    }

    Output is this.

    True
    User:user1 - PasswordExpires:False
    User:user3 - PasswordExpires:True

    As you can see the first hard code of the name gives me the True yet when its doing the compare in the foreach it doesn't and not sure why.  Any help would be appreciated.

    Thursday, May 17, 2018 7:12 PM

All replies

  • Simplify:

    $excludedUsers = @("user1","user2")
    'user1' -in $excludedUsers


    \_(ツ)_/

    Thursday, May 17, 2018 7:17 PM
    Moderator
  • I have a simple case in my original code to show it works 

    $excludedUsers = @("user1","user2")
    $excludedUsers.Contains("user1")

    I just don't see why this line doesn't work being filled in with variable.

    $excludedUsers.Contains($userName)

    Thursday, May 17, 2018 7:22 PM
  • Works fine for me.

    $excludedUsers = @(
        'SQLEXPRESS01',
        'SQLEXPRESS02',
        'SQLEXPRESS03',
        'SQLEXPRESS04',
        'SQLEXPRESS05',
        'SQLEXPRESS06',
        'SQLEXPRESS07'
    )
    Get-WmiObject Win32_UserAccount -Filter 'LocalAccount=True' |
        ForEach-Object{
            Write-Host ('User:{0} - PasswordExpires:{1}' -f $_.Name, $_.PasswordExpires)
            If($_.Name -in $excludedUsers){
                write-host 'Excluded' -fore green
            }
        }


    \_(ツ)_/

    Thursday, May 17, 2018 7:32 PM
    Moderator
  • The "-contains" operator is case INsensitive, but the "Contains" method IS case sensitive.

    Perhaps your user name contains an uppercase letter?

    $excludedUsers = @("user1","user2")
    
    # should return TRUE
    $excludedUsers.Contains("user1")
    
    # should return FALSE (upper-case "U")
    $excludedUsers.Contains("User1")


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Proposed as answer by j0rt3g4 Friday, May 18, 2018 2:35 AM
    • Unproposed as answer by jrvModerator Friday, May 18, 2018 2:37 AM
    • Proposed as answer by jrvModerator Friday, May 18, 2018 2:41 AM
    Friday, May 18, 2018 2:28 AM
  • The "-contains" operator is case INsensitive, but the "Contains" method IS case sensitive.

    NO methods in Net are case sensitive. NO identifiers in Windows are case sensitive. AD properties are case sensitive.

    Windows in a case preserving/case insensitive system by design.

    "Contains" method is case sensitive which is counter to almost all of Net.

    'user1' -in ('USER1','USER2')

    is NOT case sensitive which is why I recommended it.


    \_(ツ)_/



    Friday, May 18, 2018 2:37 AM
    Moderator
  • If there are no case-sensitive methods then how do you explain the failure when looking for "User1"?

    I'm assuming you ran that little bit of code I used as an example and verified the results?


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Saturday, May 19, 2018 2:47 AM
  • If there are no case-sensitive methods then how do you explain the failure when looking for "User1"?

    I'm assuming you ran that little bit of code I used as an example and verified the results?


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Not what I posted.  Read it more carefully.


    \_(ツ)_/

    Saturday, May 19, 2018 2:55 AM
    Moderator
  • ????

     ('USER1','USER2') -contains 'user1'


    \_(ツ)_/

    Saturday, May 19, 2018 3:00 AM
    Moderator