Summary: Guest blogger, Tim Warner, talks about using the Show-Object cmdlet.
Microsoft Scripting Guy, Ed Wilson, is here. Today I have another guest post by Tim Warner. By the way, you can meet Tim and other PowerShell enthusiasts at the Nashville PowerShell user group meeting on Wednesday, October 28. I will be there making the presentation. The Scripting Wife will also be in attendance. Also in Nashville this week is Killer Nashville. I will be speaking at that conference too, but not about PowerShell. Hope to see you in Nashville. Take it away Tim...
Spelunk [spi-luhngk], verb. To explore caves, especially as a hobby.
When I heard Jeffrey Snover use the word "spelunk" to describe PowerShell command and object discovery at Ignite 2015, I just knew I had to use it in a tutorial! Today I'm going to teach you how to use the wonderful Show-Object function from PowerShell team member, Lee Holmes, to gain insight into PowerShell pipeline objects.
Typical method for PowerShell object research
We must first recall that PowerShell pipelines are very different from, say, Linux pipelines. Rather than consisting of text, PowerShell objects are "three-dimensional" data structures that consist of properties (descriptive attributes), methods (actions the object can undertake), and events (actions that can be undertaken on the object). Collectively, these data elements are called object members.
For instance, take Get-Service. Unless you perform filtering, you receive an object collection as output. You probably know that Get-Member shows us the object's .NET Framework type name and its properties, methods, and events. Here's partial output from my Windows 8.1 administrative workstation running Windows Management Framework 5.0 Production Preview:
PS C:\> Get-Service | Get-Member
Name MemberType Definition
---- ---------- ----------
Name AliasProperty Name = ServiceName
RequiredServices AliasProperty RequiredServices...
Disposed Event System.EventHandler...
Close Method void Close()
Continue Method void Continue()
PowerShell objects are instances of underlying .NET classes, and by default those classes are self-discovering through a process known as reflection. If you want to see everything about the previous ServiceController objects, pipe the Get-Member output to Format-List like so:
Get-Service | Get-Member | Format-List *
What I've told you means that, practically speaking, we do an awful lot of get-membering and format-listing when we need to look up specific property, method, or event values. Is there (banish the thought!) an easier-on-the-eyes graphical solution?
Installing the PowerShell Cookbook module
Lee Holmes (from the Windows PowerShell development team) wrote a book for O'Reilly called Windows PowerShell Cookbook. One of Lee's "recipes" describes the Show-Object function.
You can obtain the function from the book's website (see Program: Interactively View and Explore Objects) if you want, but because I run Windows PowerShell 5.0, I'll download the script by using some of the nifty PowerShellGet commands (see Package Management for PowerShell Modules with PowerShellGet).
First we can locate the appropriate module:
Find-Module -Name *cookbook*
Cool. So Lee's module is named PowerShellCookbook, and it is stored on the PowerShell Gallery repository. Let's install it!
Install-Module -Name PowerShellCookbook -Force
As it happens, Show-Object is only one function among many that are contained in the PowerShellCookbook module. Run the following command to see them all:
Get-Command -Module PowerShellCookbook
Using the Show-Object Function
To use Show-Object, simply pipe your desired PowerShell object into the function like so:
Get-Service -Name Spooler | Show-Object
Take a look at the following Show-Object output, and I'll walk you through it.
The bottom pane (sadly unresizable) shows your ordinary Get-Member output. The upper pane allows you to quickly parse the object properties. For instance, we can expand each RequiredServices node to learn that the Print Spooler service in Windows 8.1 depends on a Remote Procedure Call (RPC) and HTTP services.
If you're of the mind to do so, you can inspect the Show-Object source code by visiting the book's website (see Program: Interactively View and Explore Objects) or by using PowerShell directly in your console:
(Get-Command -name Show-Object).Definition | Out-File 'C:\show-object-source.txt' | Notepad 'C:\show-object-source.txt'
One of the many things I love about the Windows PowerShell community is how friendly and helpful most people are. I don't want to draw comparisons to, say, the *NIX open-source community, but...well, let's just leave that alone.
Justin Rich posted an "advanced" version of Lee's Show-Object function to PoshCode that you may want to look at (see Advanced Show-Object 1.0 by Jrich523). Let me save you some work and show you the cool tree list view output from our earlier run of Get-Service:
That's all there is to it! I hope that you found this article helpful, and I thank you for reading. Take care!
Thanks, Tim, for sharing your time and knowledge. Join me tomorrow when I will talk about cool Windows PowerShell stuff.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy