Hastable where works but not quicker search

Hi
im trying to find the best way to lookup item inside hashtable:


$data1|gm
 TypeName: Deserialized.System.Collections.Hashtable

items look like this:
$data1|select -First 1

Name                           Value                                                                                                                                                                                                                                                                                                                                                                                                                                               
----                           -----                                                                                                                                                                                                                                                                                                                                                                                                                                               
manufacturer                   LENOVO                                                                                                                                                                                                                                                                                                                                                                                                                                              
id                             8e9b9c0b-da9d-4ce5-acab-226ea80f57fes                                                                                                                                                                                                                                                                                                                                                                                                                
userPrincipalName                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
lastContactedDateTime          8/4/2023 12:36:36 AM                                                                                                                                                                                                                                                                                                                                                                                                                                
azureActiveDirectoryDeviceId   f8ef7209-6965-4e11-a245-167ae1rew054cf                                                                                                                                                                                                                                                                                                                                                                                                                
enrollmentState                enrolled                                                                                                                                                                                                                                                                                                                                                                                                                                            
model                          20R6S2KE0EQW                                                                                                                                                                                                                                                                                                                                                                                                                                         
serialNumber                   PC1Q28KTRED                                                                                                                                                                                                                                                                                                                                                                                                                                            
displayName                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
systemFamily                   ThinkPad L13 Yoga Gen 1                                                                                                                                                                                                                                                                                                                                                                                                                             
purchaseOrderIdentifier        0157875611                                                                                                                                                                                                                                                                                                                                                                                                                                          
managedDeviceId                aad154d7-6323-4c7d-8d09f-83d565514702                                                                                                                                                                                                                                                                                                                                                                                                                
addressableUserName                                                                                                                                                                                                                                                                                                                                                                                                                                                                
productKey                     3305556973834                                                                                                                                                                                                                                                                                                                                                                                                                                       
groupTag                       CYTCNA                                                                                                                                                                                                                                                                                                                                                                                                                                              
skuNumber                      LENOVO_MT_20R6_BU_Think_FM_ThinkPad L13 Yoga Gen 1

this works and returns the item and its all its properties but is slow method:

$data1.({$_.'serial number' -eq 'PC1Q28KTRED'})

but when trying this:

$data1["PC1Q28KTRED"]

or this:

$data1.PC1Q28KTRED

any idea what am i missing here?

Thanks

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

What do you actually want to do?

What to you mean with “but is slow method”?

thanks for your reply

what i mean is i have data1 and data2
‘serial number’ is unique in both but data1 has one item that data2 doesnt and i need to match an object from data1(by serial) to object in $data2 and construct new list
my plan is to use this to match serial number from data1 to data2:

$FusedData = [System.Collections.Generic.List[System.Object]]::new()
foreach ($M in $data2)
{    $Current = $null
    $Current = $data1["$($m.'serial number')"]
    if ($null -ne $CurrentSP) {
        $Object = [PSCustomObject]@{
            id = $m.id
            serial   = $M.'serialnumber'
            grouptag  = $Current.grouptag
        }
        $FusedData.Add($Object)
    }
    }

as this is the fastest way to search and match within a hashtable

Please do not quote the code - format it as code! :point_up_2:t3:

Are you talking about only 2 data sets? I still don’t know what you’re trying to do. How about posting 2 sample data sets and the expected output? And please format it as code.

I will say that only once more - please format your code and sample data as code - DOT NOT USE THE BLOCKQUOTE FORMAT! ! ! ! !

The format of the two data sets looks different. How do you populate them? And what is it what you’re trying to do? Please describe the complete task - not the way you think you have to go to accomplish the task.

sorry about that , i just saw that option is not like it shows in the demo <> its in the advanced(gear) and then you have “preformat text”.
anyway i think you are right when i took data from data1 and selected on serial and id(which is what i need to match.
it finds it without problem and fast(out of 21k records).

Thanks

So it is not about two data sets?!?!? :man_shrugging:t3: And I still don’t know what you’re actually trying to do. :thinking:

so i need to combine some fields from data1 and data2
the only comkon unique thing in these two is serial number
so i can create customobject and just look for serial number either in all data1 or data2 until i get match and construct the fields i need.
using foreach and where… that is very slow method of doing this
so i was looking for faster way and its possible if i use hashtable
so i convert data2 into hastable and then run foreach on data2 but i need a fast way to match/find the serial and id fields in data1
so the fastest way to do it is by using this:

$Current = $data1["$($m.'serial number')"]

this gives me the item in data1 which the serial matches plus the ‘id’ field i need
on quick test i did it returned/matched 2000 items in less than a second :slight_smile:

:man_shrugging:t3: :man_shrugging:t3: :man_shrugging:t3:
Does that mean you solved your issue? Sorry I don’t know what you expect from us. It might be my lack of knowledge of the English language, but I did not get exactly your task yet.

What fields?

Where does data1 come from? How did you populate the data?

Where does data2 come from? How did you populate the data?

What should be the expected result?