How do I list all object property values?

Welcome Forums General PowerShell Q&A How do I list all object property values?

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

js
 
Participant
3 months, 2 weeks ago.

  • Author
    Posts
  • #104092

    Participant
    Points: 0
    Rank: Member

    Using the $ERROR object as an example, how would I iterate through each property, expand collections of properties, and get all values? I'd like to have the full output of an error and write it to a log file.
    $ERROR|get-member shows 8 properties
    $ERROR[0].CategoryInfo |Get-Member shows 5 properties
    I'm assuming a recursive loop but have not been able to get it to work.

  • #104096
    js

    Participant
    Points: 8
    Rank: Member

    I don't like how this is setup. You can't tab complete to explore it. You can do:

    $error | fl -force
    
    $error | select *

    This is the only time I've ever used fl -force.

  • #104098

    Keymaster
    Points: 7
    Rank: Member

    Also try Format-Custom and experiment with the depth parameter.

  • #104101

    Participant
    Points: 0
    Rank: Member

    Thanks for the replies. I'm looking to go further, expand properties and get more details.

    For exmaple – If I look at the most recent $Error, $Error[0], I see 9 properties.

      PS > $Error[0].psobject.properties|Select-Object name
      Name                                                                                                                                                                                                 
      ----                                                                                                                                                                                                 
      PSMessageDetails                                                                                                                                                                                     
      Exception                                                                                                                                                                                            
      TargetObject                                                                                                                                                                                         
      CategoryInfo                                                                                                                                                                                         
      FullyQualifiedErrorId                                                                                                                                                                                
      ErrorDetails                                                                                                                                                                                         
      InvocationInfo                                                                                                                                                                                       
      ScriptStackTrace                                                                                                                                                                                     
      PipelineIterationInfo 
    

    One of the properties listed is "Exception".
    If I look at properties of "Exception" I see 10 more properties.

      PS > $Error[0].Exception.psobject.properties|Select-Object name
      Name                                                                                                                                                                                                 
      ----  
      ErrorRecord
      StackTrace 
      WasThrownFromThrowStatement
      Message
      Data
      InnerException
      TargetSite
      HelpLink
      Source
      HResult
    

    I am looking to loop through each property, and expand properties where there are more properties. Like "select-object -expand" but in a recursive loop so I can explore all properties of an object.

  • #104102

    Participant
    Points: 0
    Rank: Member

    Thanks for the replies. I'm looking to go further, expand properties and get more details.

    For example – If I look at the most recent $Error, $Error[0], I see 9 properties.

      PS >; $Error[0].psobject.properties|Select-Object name
      Name                                                                                                                                                                                                 
      ----                                                                                                                                                                                                 
      PSMessageDetails                                                                                                                                                                                     
      Exception                                                                                                                                                                                            
      TargetObject                                                                                                                                                                                         
      CategoryInfo                                                                                                                                                                                         
      FullyQualifiedErrorId                                                                                                                                                                                
      ErrorDetails                                                                                                                                                                                         
      InvocationInfo                                                                                                                                                                                       
      ScriptStackTrace                                                                                                                                                                                     
      PipelineIterationInfo 
    

    One of the properties listed is "Exception".
    If I look at properties of "Exception" I see 10 more properties.

      PS > $Error[0].Exception.psobject.properties|Select-Object name
      Name                                                                                                                                                                                                 
      ----  
      ErrorRecord
      StackTrace 
      WasThrownFromThrowStatement
      Message
      Data
      InnerException
      TargetSite
      HelpLink
      Source
      HResult
    

    I am looking to loop through each property, and expand properties where there are more properties. Like "select-object -expand" but in a recursive loop so I can explore all properties of an object.

  • #104138
    js

    Participant
    Points: 8
    Rank: Member

    Hmm, weird things happen with get-content. Bruce Payette mentioned in the 2 hr ps arch video that get-content returned more than just text:

    get-content wmi.txt | fl * -force
    
    
    PSPath       : C:\Users\admin\wmi.txt
    PSParentPath : C:\Users\admin
    PSChildName  : wmi.txt
    PSDrive      : C
    PSProvider   : Microsoft.PowerShell.Core\FileSystem
    ReadCount    : 1
    Length       : 0
    
    

The topic ‘How do I list all object property values?’ is closed to new replies.