Get-Member: Powershells most powerfull cmdlet!


Get-Member is by far my favorite and most used cmdlet. It allows you to so what you can do or get from an object.

Before I get in to the meat of the cmdlet, I want to go over some of the terms, since I know there are a lot of admin type people learning powershell and this is more so a developer concept.

Powershell (and .NET) works entirely in objects. Even a simple string is an object, so what’s an object?

An Object is an instance of a class, so then, what’s a class?

The class is what defines the object. It tells you what you can do with your objects and how its done. It’s the Members that let you work on the object.

Members are the Properties, Methods and Events of an object that let you manipulate it. There are other member types, but those are the primary ones.

Lets take a look at a simple String object

$str = “Hello World”

$str | gm

We’ll see it print out a TypeName and a list of members, something like this.

TypeName: System.String

Name             MemberType            Definition
—-             ———-            ———-
Clone            Method                System.Object Clone()

Length           Property              System.Int32 Length {get;}

 

The first thing that should catch your eye is the TypeName, which is the .NET class that the object is, in this case it’s a System.String

If you have Shay’s Get-MSDNInfo function then you can easily get to the MSDN doc’s for an object of this type.

Get-MSDNInfo $str

At any rate, now that we can see the members of this object, we have a pretty good idea of what we can do with it. In the case of a System.String we’ll see that we mostly have methods available to us. You can access any of the members of an object with dot notation.

$object.Member

$str.ToUpper()

Perhaps you can already see why this is my favorite cmdlet. If I run a cmdlet that someone else created or if I am working with some .NET stuff and I get an object back and I don’t know what type it is or what I can do with it, I just toss it to Get-Member and it gives me what I am able to do with that object.

A WORD OF CAUTION!

If you have a collection of something, lets say a collection of strings, you’ll notice that depending on how you call Get-Member (shorthand being gm) you’ll get a different return.

$str = @("Hello","World")
$str | gm
gm –InputObject $str

In this case, powershell assumes you know you have an array, and is showing you what is in that array, not the array object itself. So in the first case, $str | gm you’ll see the TypeName is System.String, which is what is in the array, basically the same as doing $str[0] | gm

On the other hand, if you pass the object Get-Member via the inputobject parameter, you’ll get the TypeName of the collection, in this case it’s a generic System.Object[]. One important thing to note on that type name is the [] at the end, which is what indicates this is a collection.

In the case of a collection of strings, its not so important, but lets say you are using a  DirectorySearcher and use the FindAll() method, what you actually get back is a System.DirectoryServices.SearchResultCollection so depending on how you use Get-Member it will either tell you it’s a SearchResultCollection or a SearchResult, with each having different methods and properties to work with.

 

When in doubt, do it both ways.

 

There is one other thing to note here. When you do $object | gm, Get-member will show you each UNIQUE datatype in a collection, so for example, if you do the follow.

$arr = 1,"hello"
$arr | gm

You’ll get two TypeNames back, System.Int32 and System.String.

You’ll see this more so if you do a Get-Member on a Get-Eventlog. With Event Logs and WMI you get the namespace with it, I havent been able to learn why that is yet.

 

I hope this helps you spend more time in powershell and less time googling object different objects!

Advertisements

About jrich

I am the Solutions Architect for Apex Learning in Seattle WA. I've been working with computers since I was 13. Started programming when I was 14. Had my first IT job as tech support at an ISP at the age of 15 and became a network admin at the age of 17. Since then I've worked at a variety of small to mid size companies supporting, maintaining and developing all aspects of IT. Mostly working with Windows based networks but have recently been working with Solaris system as well. I created this blog mostly as a place for me to take my own notes, but also share things that I had a hard time finding the info for.

Posted on June 20, 2011, in WMF (Powershell/WinRM) and tagged , , . Bookmark the permalink. 1 Comment.

  1. The reason why sometimes you get the collection and sometimes each element?
    To quote Keith Hill’s blog:
    …when you send a collection down the pipe, PowerShell will automatically “flatten” the collection meaning that each individual element of the collection is sent down the pipe, one after the other.
    And Microsoft about_pipelines:
    When you pipe multiple objects to a command, Windows PowerShell sends the objects to the command one at a time. When you use a command parameter, the objects are sent as a single array object.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: