Display-Object: a PowerShell utility Cmdlet

How do you list all the objects and values within a PowerShell object, investigate an object or explore its structure? There must be a simple way. I used to use ConvertTo-JSON. This is fine up to a point but what if you just wish to search for strings or look for objects with a cartain name or value? What if you need their path so you can reference them? It isn’t plain-sailing. In the ISE, the intellisense will help you a lot but I want more and I want to do it in script rather than the IDE

Let’s start with a silly example just to get in the mood. Imagine we have a hashtable that tells us our servers and users.

If we just pipe it to Format-Table we get this, which is a good start, but those values are more tantalising than helpful

Getting the names of the base members isn’t that helpful. After all,  we can also get the names of the servers through Get-Member

… Or, knowing it is a hashtable, use …

If we can assume that it is a hashtable, we can get name and value as we did with Format-Table

Well. That’s progress of a sort in that we have something we can work on, but of course, not every object will be a hashtable.  but we can elaborate it to list more types of object by turning a hashtable into a pscustomobject (Beware: the order of properties isn’t preserved). We’ll output a PSCustomObject as well so that we can output the results or do filters on them.

We changed that hashtable into a PSCustomPbject so we could iterate through the names using the Noteproperties. We get those noteproperties via the Get-Member (gm) cmdlet. We added a $ Reference to represent whatever name you used for the variable that referred to your object.

the problem here is that this trick only works for a pscustomObject or something we can change into it, such as a hashtable or ordered dictionary. For other objects, we  use the property members rather than the NoteProperty members.

You can see that we need recursion because all the values are themselves objects of some description. Most data is more than a simple hashtable or pscustomObject.

 The problem here is that it doesn’t yet handle arrays that well.  We also need to allow the function to avoid a list of one or more names and allow it to work with a range of objects such as XML objects.  It is also wise to copy ConvertTo-JSON in the way that it specifies the allowable depth. As a final touch, we allow it to keep its cool when presented with write-only values.

Naturally, one can tinker with the values you return. Some people will want a ‘name/index’ column. I’ve kept it simple. I like to know the path because that tells me the structure and it provides the correct dot syntax to get the value from the object.

You just substitute the name of the variable representing the objects for the $ symbol in the path.

You can filter what comes back. What server does Tony use?

What are the server locations?

Let’s try a text file..

We can display other, meatier, objects such as the PowerShell process

(too much to display in a Blog!)

What if you just wanted to look at an object  in the depths of a large object?


Once you can ‘walk’ through an object and search them some possibilities open up. You can slice and dice them; You can convert objects to markup or find out if they’ve changed and how. If you are in a DevOps Windows environment, you’re likely to have all sorts of objects that are delivered to you by cmdlets that monitor the servers. It is useful to be able to investigate them and quickly learn how to pull out just the data you need.  Hopefully, I’ll be able to show some of these in this series of Blog posts.

This function saved me a great deal of time when trying to get  the result of a regex match into a rational format, so I have a certain affection for it.

I’ve added the source to a collection of PowerShell utilities on my github repositories. I’ve done a couple of fixes since this was first published. Let me know of any good ideas for enhancements!