How do I get the TypeName of an object piped to Get-member?

Welcome Forums General PowerShell Q&A How do I get the TypeName of an object piped to Get-member?

Viewing 2 reply threads
  • Author
    Posts
    • #215565
      Participant
      Topics: 86
      Replies: 138
      Points: 677
      Rank: Major Contributor

      Hello,
      When I pipe an object to Get-Member, I get a very useful listing of methods, properties, etc. At the very top, i.e., the first line of the output is something that begins with info on the object’s type. Examples:

      TypeName: System.Data.Dataset
      TypeName: System.String
      

      I can see a GetType() method in the Get-member output list, but how do I invoke it to get just that first line (assuming this is the correct method needed)?

      Would be grateful for any tips or suggestions.

       

      • This topic was modified 4 months ago by Ramon TAN.
    • #216171
      Participant
      Topics: 4
      Replies: 480
      Points: 1,755
      Helping Hand
      Rank: Community Hero

      I guess this might work. My testing shows the same short name as the type that get-member does except with arrays it can show object. Not sure why.

      $oht = [ordered]@{Hash='value';Table='value'}
      
      $oht.GetType().name
      OrderedDictionary
      
       
      
      $arr = New-Object System.Collections.ArrayList
      
      $arr.Add('value')
      
      $arr.GetType().name
      ArrayList
      
       
      
      $arr = @(@{Hash='value';Table='value'})
      
      $arr.GetType().name
      Object[]
      
      

       

      I believe there can also be a difference between

      $variable | Get-Member
      
      

      and

      Get-Member -InputObject $variable
      
      

       

      Another thing to note is an array may contain multiple types

      $arr = @(1,'string')
      
      $arr.ForEach({$_.gettype().name})
      Int32
      String
      
      

       

      I hope this helps.

    • #216231
      Participant
      Topics: 86
      Replies: 138
      Points: 677
      Rank: Major Contributor

      Many thanks Mr Maurer, for your answer/solution with thought-provoking examples. While I was struggling with the GetType() within Get-member, I came up with the ff:

      b$ = "a string"
      b$ | Get-member | Out-string -Stream | Select-string "Typename:" -Noemphasis
      

      Obviously, my mindset was set on strings rather than objects.

      Thanks again for your response.

Viewing 2 reply threads
  • The topic ‘How do I get the TypeName of an object piped to Get-member?’ is closed to new replies.