Accessing Hash Table Elements

Welcome Forums General PowerShell Q&A Accessing Hash Table Elements

This topic contains 6 replies, has 3 voices, and was last updated by

4 years, 2 months ago.

  • Author
  • #19720

    Points: 0
    Rank: Member

    Powershell Newbie here: I'd like to access just a couple of elements from what I think is a Hash table... pairs..... to work with them further; in particular, the contactID, lastName, and FirstName.

    I've got my data in an object $BPhash which I originally received in the form of a JSON response, which now shows up as below.. (example of a single record, but I have 200 records). So, far I can't figure out how to access the contents of a single record in sequence so I can get 598, MCPHERSON, KAREN ...

    contactID lastName FirstName

    and so on.

    If I execute $BHHash.contactID, it dutifully prints out all 200 contactIds. and if I execute $BPhash.lastName it prints out all of the lastnames

    Example for one record:


    contactId : 598
    isPrimaryContact : True
    salutation :
    firstName : KAREN
    lastName : MCPHERSON
    postAddressIds : @{DEF=513; DEL=513; BIL=513}
    communication : @{emails=; telephones=; messagingVoips=; websites=}
    contactStatus : @{current=}
    relationshipToAccount : @{isSupplier=False; isStaff=False}
    marketingDetails : @{isReceiveEmailNewsletter=True}
    financialDetails : @{priceListId=2; nominalCode=0; creditLimit=0; creditTermDays=0; currencyId=1; discountPercentage=0.00}
    assignment : @{current=}
    organisation : @{organisationId=598; name=ROSENTHAL MONTESSORI ELEMENTARY}
    createdByid : 0
    createdOn : 2014-03-17T10:02:54.000-04:00
    updatedOn : 2014-03-17T10:02:55.000-04:00
    contactTags : 8


    If I do a Get-Member I get the following

    PS C:> $BPhash | gm

    Name MemberType Definition
    —- ———- ———-
    Equals Method bool Equals(System.Object obj)
    GetHashCode Method int GetHashCode()
    GetType Method type GetType()
    ToString Method string ToString()
    assignment NoteProperty System.Management.Automation.PSCustomObject assignment=@{current=}
    communication NoteProperty System.Management.Automation.PSCustomObject communication=@{emails=; telephones=; messagingVoips=; websit...
    contactId NoteProperty System.Int32 contactId=399
    contactStatus NoteProperty System.Management.Automation.PSCustomObject contactStatus=@{current=}
    contactTags NoteProperty System.String contactTags=8
    createdByid NoteProperty System.Int32 createdByid=0
    createdOn NoteProperty System.String createdOn=2014-03-17T10:02:46.000-04:00
    financialDetails NoteProperty System.Management.Automation.PSCustomObject financialDetails=@{priceListId=2; nominalCode=0; creditLimit=...
    firstName NoteProperty System.String firstName=WENDY
    isPrimaryContact NoteProperty System.Boolean isPrimaryContact=True
    lastName NoteProperty System.String lastName=MENDOLA
    marketingDetails NoteProperty System.Management.Automation.PSCustomObject marketingDetails=@{isReceiveEmailNewsletter=True}
    organisation NoteProperty System.Management.Automation.PSCustomObject organisation=@{organisationId=399; name=THE TOT SPOT}
    postAddressIds NoteProperty System.Management.Automation.PSCustomObject postAddressIds=@{DEF=314; DEL=314; BIL=314}
    relationshipToAccount NoteProperty System.Management.Automation.PSCustomObject relationshipToAccount=@{isSupplier=False; isStaff=False}
    salutation NoteProperty System.String salutation=
    updatedOn NoteProperty System.String updatedOn=2014-03-17T10:02:46.000-04:00


    Any help would be appreciated. Thanks!

    —- Larry

  • #19721

    Points: 0
    Rank: Member

    It looks like what you're actually dealing with is an array of objects, not a hashtable. Try typing this command, to make sure:


    Assuming that this is an array, you could do something like this:

    $BHHash | Where-Object { $_.contactID -eq 598 }
  • #19723

    Points: 0
    Rank: Member

    Hi, Dave......

    Ok, If I execute $BPHash.GetType().FullName I get

    So, you are correct, it *is* an array at least.

    For example, if I execute


    It will print out all elements of a single record. (as in the example above)...


    $BPHash | Where-Object { contactID -eq 598 }

    Returns an error:

    ContactID : The term 'contactID' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of
    the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:26
    + $BPHash | Where-Object { contactID -eq 598 }

    Hmmmmm ..

  • #19724

    Points: 0
    Rank: Member

    Whoops, mixing up my syntax. 🙂 It should be either:

    $BPHash | Where-Object contactID -eq 598
    # Or:
    $BPHash | Where-Object { $_.contactID -eq 598 }

    The former only works on PowerShell 3.0 or later; the second option works in all PowerShell versions.

  • #19725

    Points: 0
    Rank: Member

    Wow.....fantastic. Many thanks. That works!

    Now, the one thing... how about if I just want to get the three elements:
    contactID, lastName, and firstName.

  • #19728

    Points: 24
    Team Member
    Rank: Member


    You can pipe into the Select-Object cmdlet and specify the properties you're interested in.

    $BPHash | Where-Object contactID -eq 598 | Select-Object -Property contactID, lastName, firstName
    # Or:
    $BPHash | Where-Object { $_.contactID -eq 598 } | Select-Object -Property contactID, lastName, firstName
  • #19730

    Points: 0
    Rank: Member

    Daniel....and Dave,

    Thank you both so much for your help! This is major progress for me.

    best wishes,

    —- Larry

The topic ‘Accessing Hash Table Elements’ is closed to new replies.