The Surly Admin

Father, husband, IT Pro, cancer survivor

Powershell Discovery: Variables

This is true of any programming/scripting language.  Variables are the life blood of what you do.  Without them there is really nothing you can do in a scripting language.  I’m going to assume you know all about variables so I won’t get down to what they are.  But one of the fun things about variables is all the different things you can store in them.  Objects, datetimes, integers pretty much anything can be stored in them these days.  And Powershell has a truly dizzying array of variable types to work with.  Figuring out what you’re dealing with is key to a successful script.  There are a lot of things you can do to an object, but not all objects are created equal!  So some objects will allow you to Convert them to datetime, and others won’t.  How do you find out?


This is a method that all variables in Powershell share.  It will simply ask Powershell what TYPE of variable this is.  What do I mean?  Consider this:

[string]$a = "Good morning!"

And here’s what you get as a result:

IsPublic IsSerial Name   BaseType
-------- -------- ----   --------
True     True     String System.Object

Under name you see it’s a string type.  Now let’s do something a little fancier.  Let’s use the Get-Service cmdlet and save the results into a variable and see what we get.

$b = Get-Service Spooler

IsPublic IsSerial Name              BaseType
-------- -------- ----              --------
True     False    ServiceController System.ComponentModel.Component

So you can see the Name is ServiceController, which is a very specific type of object related to services.  Now we have an object of the ServiceController type stored in our $b variable the next question is what can we do with it?


Using Get-Member we can then look at all of the properties that object has to offer, as well as several methods that are available.  A method is just simply a task that can be performed on the object.  Since Get-Member is a cmdlet and not a property we can’t call it by just doing $b.Get-Member.  We have to pipe the results of $b into Get-Member.  I’ll talk about pipelining in another post.

$b = Get-Service Spooler
$b | Get-Member

Cool, huh?  What does it mean?  Name is the name of the property or method, the “type” is MemberType and then a quick little “definition” (if you want to call it that).  Methods are things you can do with the object, properties are information about the object.  One key you want to look for is at the end of the definition in parenthesis is “get” and “set”.  Get properties are read-only, but if you see “get;set” it also means you can change the property.

Want to see the status of Spooler (which is currently stored in $b)?  Want to stop and start the service?


As you can see, Get-Member is the gateway to what you can do with Powershell.  Load a variable with your favorite cmdlet and pipe it into Get-Member and you will be able to look at all of the properties and methods available through the use of that object.

Armed with Get-Command, Get-Help, GetType() and Get-Member you can begin to explore Powershell without constant Googling, reading and trial and error.  Next up I’ll talk about the ForEach statement, the ForEach-Object and the IF statement.


September 25, 2012 - Posted by | Powershell - Getting Started | , , , ,


  1. […] have array’s of objects.  Remember how we had loaded a variable with a single object in the last post?  What if we loaded that variable with ALL of the […]

    Pingback by Get Started with Powershell: Loops and branches « The Surly Admin | September 26, 2012 | Reply

  2. […] Integrated Scripting Environment (ISE) like PowerGUI or PowerShellPlus, but then I discovered a few other commands that helped me discover information about the objects I was working with.  Specifically […]

    Pingback by Discovery with Powershell – Script to Get Screenshots for VMs « The Surly Admin | January 31, 2013 | Reply

  3. […] do you start using Powershell? Powershell Discovery: Variables Get Started with Powershell: Loops and branches Powershell and the Pipeline Powershell Best […]

    Pingback by Powershell – Steep Learning Curve? « The Surly Admin | March 28, 2013 | Reply

Leave a Reply

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

You are commenting using your 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: