System.Collections.Hashtable vs @{key=value} as return of the same code

Welcome Forums General PowerShell Q&A System.Collections.Hashtable vs @{key=value} as return of the same code

  • This topic has 2 replies, 3 voices, and was last updated 1 month ago by
Viewing 2 reply threads
  • Author
    • #264650
      Topics: 10
      Replies: 11
      Points: 148
      Rank: Participant


      I have script which scans local network and get some informations from hosts. For this I use below code:

      I wrote two sections, one for PowerShell greater than than 5, and second section for older versions of PowerShell (for example ver 2 in Windows 7). This is because version older than 5 cannot use “Get-CimInstance” command.

      If it’s scanned computer with Win 10 and PS 5 it returns hashtable:

      And when is scanned computer with Win 7 and PS 2 it returns object System.Collections.Hashtable.

      Why is that?

      Best Regadrs


      Please look at screen below:

    • #264671
      Topics: 2
      Replies: 382
      Points: 523
      Helping Hand
      Rank: Major Contributor

      The [PSCustomObject] type accelerator was introduced in version 3.  I expect version 2 is ignoring the type declaration and just creating a hashtable.

      If you pipe it to Get-Member you should find that Windows 10 is returning an object of type System.Management.Automation.PSCustomObject

      For version 2, I think you’re going to have to revert to New-Object and Add-Member.

      • This reply was modified 1 month, 2 weeks ago by Matt Bloomfield. Reason: Formatting
    • #266489
      Topics: 5
      Replies: 177
      Points: 686
      Helping Hand
      Rank: Major Contributor

      For version 2 it can be a little simpler by using the hashtable and not doing Add-Member for each property.  In fact this works for newer versions of PS as well so you could move this outside of your if statement and do for all like this:


Viewing 2 reply threads
  • You must be logged in to reply to this topic.