PowerShell

Guy recommends:
Free config generator

Solarwinds Config Generator

This CG will put you in charge of controlling changes to network routers and other SNMP devices.

Download your free Config Generator



Windows PowerShell Get-Member

Introduction to Windows PowerShell Get-Member

Get-Member is an essential command for discovering more about a PowerShell objects.  Because you are at the command line, you cannot right click an object and check its properties; instead, what you can do is type: Get-ObjectXYZ | Get-Member.

PowerShell is sometimes referred to as a self-describing language.  Indeed, it is thanks to Get-Member that we can see the properties and methods associated with any given object.

Topics for PowerShell's Get-Member

 ♣

The Concept Behind Get-Member

The reason that I use Get-Member at every opportunity is so that I can discover 'handles', which help me achieve a particular scripting task.  These 'handles' or memberTypes are split into methods and properties.  Suitable objects to learn more about Get-Member include, service, process, eventlog, or WmiObject.

Here are three examples to illustrate my research technique:

Get-Service 
# Results: A long list of Windows services

Get-Service | Get-Member
# Result: A comprehensive list of properties and methods

Get-Service messenger | Get-Member
# Reveals more properties specific to the messenger service

PowerShell Tip - use the tab for Auto-completionTry judicious use of 'tab' and invoke Auto-completion, for example try, Get-p [Tab].  If you press tab again PowerShell cycles through commands beginning with Get-p, for example Get-Process, Get-PSDrive

The Basics of Verb-ObjectXYZ | Get-Member

Employing Get-Member is a useful tactic in the bigger game of scripting specific properties.  For example, if your goal is to stop, or to start a service, then you need to investigate the scripting properties, and possible values, for that service.  A little research with Get-Member will reveal a property called 'Status', with values of 'Running' or 'Stopped' - perfect for our task.

Naturally, the phrase Get-Member never varies, a case of learn once and apply to many objects.  Just remember the hyphen, and also remember that there are no spaces in verb-noun pairs. 

Troubleshooting Get-Member

Correct Syntax:  Get-Member

Incorrect Syntax: get member (no hyphen), or get -Member or even Get- member (spaces)

As I mentioned earlier, when you use Get-Member in conjunction with other commands such as Get-ObjectXYZ, remember the 'Pipe' or 'Pipeline' symbol.  This vertical line | is ASCII 124 and looks like this at the PS Prompt  ¦

Correct Sequence: Get-ObjectXYZ | Get-Member 

Incorrect Syntax: Get-Member | Get-ObjectXYZ (Get Member should receive the output)
Trap:  Get-Member Get-ObjectXYZ (Forgot the pipe |)

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.  Fortunately, Solarwinds have created the WMI Monitor so that you can examine these gems of performance information for free.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

More Examples of Get-Member

# PowerShell's Get-Member with WMI classes
Get-WmiObject Win32_processor | Get-Member

Note 1:  Get-Member is even more useful with WmiObject because its object classes vary more than objects such as Service, Process or Eventlog.  In the above example Win32_processor is the WMI class.

PowerShell Tip - Try alias such as gwmiTry aliases, for example gwmi for Get-WmiObject.  Many people use the alias 'gm' instead of the full cmdlet: 'Get-Member'.

Get-Process | Get-Member

Get-Eventlog system | Get-Member

Note 2:  Get-Eventlog | Get-Member would be incorrect.  If you don't tell PowerShell which eventlog you want, the command does not complete.  The log in the above example is 'System', you could substitute 'Security' or 'Application'.  For a full list try: Get-Eventlog -list.

Filter Get-Member with -Membertype

Results from the simple command: Get-Member, may produce too many MemberTypes; if so, then you can filter the output with a -Membertype argument, for example:

Get-Process | Get-Member -Membertype property
# or
Get-Process | Get-Member -Membertype method

Appreciate that the results of the parameter -MemberType are grouped into at least four categories:  AliasProperty, Method, Property and ScriptProperty.  Once you have researched -MemberType, you may see new applications for the properties that it reveals, for example

Get-Process | group company

Strictly speaking, the above command should be, 'group-Object company', however, 'group-Object' has an alias, thus you can shorten the command to plain 'group'.

Here is a method for expanding the company information:

Get-Process | sort company | ft -group company name, description -auto

Note 3: ft is an alias for format-Table.

Incidentally, this whole page of -Member examples gives valuable experience of when to use the hyphen - also called a dash, and sometimes referred to as a minus sign.  For example -Membertype takes the hyphen prefix, whereas property, as in -Membertype property does not need any prefix.

Get-Member Alias GM

With Microsoft, there are always at least three ways of doing everything, what seems like redundancy when you are an expert, seems like perspective when you are a beginner.  One obvious example is that you can abbreviate format-Table to ft.  As you increase your range of PowerShell commands, keep an eye out for another PowerShell Alias, for example gci (Get-Childitem).

Guy Recommends: SolarWinds Engineer's Toolset v10Engineer's Toolset v10

The Engineer's Toolset v10 provides a comprehensive console of utilities for troubleshooting computer problems.  Guy says it helps me monitor what's occurring on the network, and the tools teach me more about how the system itself operates.

There are so many good gadgets, it's like having free rein of a sweetshop. Thankfully the utilities are displayed logically: monitoring, discovery, diagnostic, and Cisco tools.  Download your copy of the Engineer's Toolset v 10

Getting Help for Get-Member

For a complete list of filters supported by the Get-Member command, call for help thus:

# Help for PowerShell Get-Member
Get-Help Get-Member -full

This may be just me, but I have the urge to call this -method instead of -Member.  I mention this because you always learn more from mistakes.  The answer lies in, a) Reading the error message!  b) Trying an example you know works, for example, I was trying this:

Here is a mistake that I made:

Get-WmiObject win32_computersystem | Get-method

When I read the error message it said: 'Get-method is not recognised'.  Hmmm... I thought, let me try an old friend the process object.

When I tried
Get-Process | Get-method and this also failed, I realized that I was having a 'senior moment'.  Fortunately I woke up and read the error message slowly. 'Get-method is not recognised'.   Ah ha, Get-method is what's wrong, why don't I try Get-Member.  Perfect, it worked just as I wanted.  Thank you error message.

Guy Recommends: SolarWinds LANSurveyorSolarwinds LANSurveyor

LANSurveyor will produce a neat diagram of your network topology.  But that's just the start; LANSurveyor can create an inventory of the hardware and software of your machines and network devices.  Other neat features include dynamic update for when you add new devices to your network.  I also love the ability to export the diagrams to Microsoft Visio.

Finally, Guy bets that if you take a free trial of LANSurveyor then you will find a device on your network that you had forgotten about, or someone else installed without you realizing!

Download a Free Trial of LANSurveyor

New Get-Member Parameters in v 2.0

Two new parameters are -view and -force.  In truth, I have yet to find a cmdlet where researching with these new parameters makes a difference.

Get-Member -Static

Get-Member has a switch to display just static properties.  It's only useful on certain objects, for example, [DateTime]

# Example of Get-Member -Static
[DateTime] |Get-Member -MemberType Property -Static

MaxValue Property static System.DateTime MaxValue {get;}
MinValue Property static System.DateTime MinValue {get;}
Now Property System.DateTime Now {get;}
Today Property System.DateTime Today {get;}
UtcNow Property System.DateTime UtcNow {get;}

Get-Help and Get-Member Working in Tandem

Remember that Get-help and Get-Member are complimentary.  On the one hand, Get-help will disclose information about parameters or switches that you can employ with your command, for example -recurse with Get-Childitem -path and -pattern with Select-String. 

On the other hand, Get-Member will disclose the properties and methods, for example .extension .fullname both of which are useful with Get-Childitem.  My point is that Get-help and Get-Member each provide different information; they are complimentary rather than interchangeable.  If you are complete beginner, investigate both Get-help and Get-Member; when you are an intermediate don't get fixated on one, and forget all about the other.

More Examples of Get-Member

Summary of PowerShell's Get-Member

When you need to investigate the methods and properties available to a PowerShell object, then call for Get-Member. You will soon get used to its hyphen and associated 'Pipe' symbol (|), just remember the correct sequence: Get-ObjectXYZ | Get-Member.

See more PowerShell examples for syntax advice

PowerShell Tutorials  • Syntax  • PowerShell functions  • Plist  • RegEx

Get-Date  • Quotes  • PowerShell variables  • Conditional operators  • Hashtable

Please write in if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Google

WebThis Site

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, Solarwinds have created the WMI Monitor so that you can examine these gems of performance information for free.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

 

Home Copyright © 1999-2010 Computer Performance LTD All rights reserved

Please report a broken link, or an error.