Prof. Powershell

A PowerShell Object Lesson Refresher

PowerShell doesn't require that you become a developer, but you do have to get the basic concept of objects. It's simple, really. Let's revisit, if you don't object.

You can't fully appreciate PowerShell and what it can do for you until you get your head around the concept of an object-based shell. PowerShell commands and expressions work with and create objects (not text) as is the case in almost every other shell. This doesn't mean you need to be a developer to use PowerShell, but if you can understand a few basic object concepts, you'll go far.

One of the first things you should do after reading this lesson is to take another few minutes reading the About_Objects help topic in PowerShell.

PS C:\> help about_objects

In simple terms, an object is a black box that has attributes or properties that describe it. Some of these properties you can read-only. Others you can change or set. Consider a service. It has properties like name, displayname, status and services it depends on.

Often, objects can also be made to do something. These are referred to as methods. Sometimes the method is used to modify the object and sometimes to make an external change. A service can be stopped and started. You can also modify the service object by changing its start mode, whether it is automatic, manual or disabled.

To discover an object's properties and methods you will use the Get-Member cmdlet. To discover what a service object looks like, we'll run a cmdlet to retrieve service:

PS C:\> Get-Service

Yes, you see text output but that's because you're not a machine. These are really objects, so we'll tell PowerShell to take those objects and hand them to Get-Member:

PS C:\> Get-Service | Get-Member

See the methods and properties? See that some properties are read-only {get} and some you can change {get;set}? Again,don't feel you need to be a developer to work with these objects. More often than not, there is a cmdlet that handles working with methods in a very simple manner. To stop a service you don't need to get the service and call the Stop() method. Use a cmdlet designed for that purpose:

PS C:\> stop-service wuauserv

You don't see anything but the service has been stopped. I'll explain why next time.

Working with objects means we can accomplish a great deal with a minimum amount of effort:

PS C:\> Get-Service -Name "wuauserv","wsearch","winrm" | Restart-Service -PassThru

With this single command, I retrieved the three services and restarted all of them with no scripting or programming.

Objects are your friends, so get to know them better.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular