1

When querying for the properties of a Power Shell object, I want to know if the properties listed have child properties that might provide me useful info, but neither the Get-Member command nor the Select-Object -ExpandProperty parameter offer me a way to get that information for all the properties up front.

For instance, if I execute a "Get-Member -MemberType Property" command against an instance of an X509 certificate object, I get a list of 18 properties including "Archived", "Extensions", "FriendlyName", etc.

Most of those properties do not have child properties, but at least one - the "Extensions" property - does.

In turn, some of those child properties have their own child properties.

I need to get all that info up front on one query, not experiment with each one to see if I discover something interesting.

Is there a way to get this info or has someone written a query that will display all the child properties of top-level properties?

I've looked around quite a bit and not found anything.

I tried scripting out a query, but thus far it has not produced good results.

Thank you.

F.S.
  • 71
  • 1
  • 4
  • `Extensions` sounds like a collection of some kind. What would you like such a query to do when it encounters a collection? Output the properties of the collection itself? Output the properties of each item in the collection? Both? – Lance U. Matthews Jan 14 '20 at 06:08
  • Does this answer your question? [How to recursively enumerate through properties of object?](https://stackoverflow.com/questions/14970079/how-to-recursively-enumerate-through-properties-of-object) – Lance U. Matthews Jan 14 '20 at 06:16

3 Answers3

1

I usually accomplish what you seek simply by converting to json.

By default, ConvertTo-Json have a depth of 4 elements. Since you only want the top properties and their child, you can reduce the -depth to 2.

#Selecting the first certificate just for demonstration purposes.
$YourObject = (get-childitem -Path 'Cert:\CurrentUser\CA\')[0]

# This will work with any objects.
$YourObject | ConvertTo-Json -Depth 2

Here's a partial look at the resulting query:

{
    "Archived":  false,
    "Extensions":  [
                       {
                           "Critical":  false,
                           "Oid":  "System.Security.Cryptography.Oid",
                           "RawData":  "48 33 48 31 6 8 43 6 1 5 5 7 48 1 134 19 104 116 116 112 58 47 47 115 50 46 115 121 109 99 98 46 99
111 109"
                       },
                       {
                           "CertificateAuthority":  true,
                           "HasPathLengthConstraint":  true,
                           "PathLengthConstraint":  0,
                           "Critical":  true,
                           "Oid":  "System.Security.Cryptography.Oid",
                           "RawData":  "48 6 1 1 255 2 1 0"
                       },

(I only pasted a small snippet)

You can easily see the properties such as Extensions, their child properties and the associated values.

Additional note

If you omit the -depth parameter for ConvertTo-Json, it will recurse to a depth of 4.

The depth is configurable up to 100 levels. That being said, some object will have properties that recurse upon the object, so unless needed, you should not unnecessarily put the maximum value there.

Sage Pourpre
  • 9,932
  • 3
  • 27
  • 39
1

Something like this is the "nuclear option" for me. Usually I don't find what I want anyway:

get-process cmd | fc * | findstr /i whatever

See also How to list all properties of a PowerShell object

js2010
  • 23,033
  • 6
  • 64
  • 66
0

I typically use Format-Custom with a -Depth on the properties you want to expand. Here's an example of displaying only the Extensions property.

Changing the default $FormatEnumerationLimit value to -1 will also allow you to display all enumerable property values:

# Remove limit from enumeration limit which is 4 by default
$FormatEnumerationLimit=-1

# Use Format-Custom with the depth required:
Get-ChildItem -Path 'Cert:\CurrentUser\CA\' | Select-Object -First 1 -Property Extensions | Format-Custom -Depth 1

Output:

class X509Certificate2
{
  Extensions = 
    [
      System.Security.Cryptography.X509Certificates.X509BasicConstraintsExtension
      System.Security.Cryptography.X509Certificates.X509Extension
      System.Security.Cryptography.X509Certificates.X509KeyUsageExtension
      System.Security.Cryptography.X509Certificates.X509Extension
      System.Security.Cryptography.X509Certificates.X509Extension
      System.Security.Cryptography.X509Certificates.X509Extension
      System.Security.Cryptography.X509Certificates.X509SubjectKeyIdentifierExtension
      System.Security.Cryptography.X509Certificates.X509Extension
    ]

}
Glenn
  • 1,687
  • 15
  • 21