Q:

Powershell where slow is there a faster way to search object?

i have the following code:
$HardCatUsers = Import-Csv z:\Scripts\Powershell\Hardcat_People_Export.csv

$ADUsers = Get-ADUser -Filter {enabled -eq $true -and surname -like "*" -and    GivenName -like "*" -and Name -notlike "AD*" -and Name -notlike "LRTM*"} -SearchBase "ou=Production, dc=diti, dc=lr, dc=net" | Sort-Object Surname
foreach ($ADUser in $ADUsers)
{
    if ($ADUser.Name.length -le 8)
    {
        $ADSearch = "$($ADUser.Surname), $($ADUser.GivenName) - $($ADUser.Name.ToUpper())"
        if ($Code = $HardCatUsers | Where {$_.Description -eq $ADSearch})
        {
            #"$($ADSearch) $($Code)"
        } else { 
            "$($ADSearch) NEW"
        }
    }

}
$hardCatUsers object is just two columns of data like:
Description                                                     Code                                                          
-----------                                                     ----                                                          
# No Assigned User - ACTIVE #                                   ASSET MANAGEMENT                                              
# No Assigned User - SPARE #                                    NO USER SPARE                                                 

what i am doing is pulling a filtered list of AD users back and formatting three attributes, then comparing / lookup this string in the $hardcatusers if not found just outputting the string.
but the current implementation seems to be very slow on each lookup ie 5 seconds. is there a more elegant way to search the list for matches?
Thanks

A:

I found that a foreach loop with the $hardcatusers is much faster than the where
ie:
foreach ($ADUser in $ADUsers)
{
    if ($ADUser.Name.length -le 8)
    {
        $found = $false
        $ADSearch = "$($ADUser.DisplayName) - $($ADUser.Name.ToUpper())"
        foreach ($HardcatUser in $HardCatUsers)
        {
            if ($HardcatUser.Description -eq $ADSearch)
            {
                $found = $true
            }
        }

        if ($found -ne $true)
        {
            $Results = New-Object PSObject -Property (@{
                                Description = $ADSearch
                                Code  = "PE" + $NextCode.ToString("000000")
                    })

            $Result += $Results
            $NextCode = $NextCode + 1               
        }

    }

}

