Accessing Hash Table Elements

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 …

(pre)
contactID lastName FirstName
59 MCPHERSON KAREN
60 SCHMOE JOE
61 SMITH MARY
(/pre)

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:

(pre)

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

(/pre)

If I do a Get-Member I get the following

(pre)
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

(/pre)

Any help would be appreciated. Thanks!

---- Larry

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

$BHHash.GetType().FullName

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

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

Hi, Dave…
Thanks!

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

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

For example, if I execute

$BPhash[3]

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

However,

$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 …

Whoops, mixing up my syntax. :slight_smile: 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.

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.

Larry,

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

Daniel…and Dave,

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

best wishes,

---- Larry