With over 70 PowerShell classes under my belt, I have a good idea of where most IT Pros struggle when making the mind shift to the PowerShell world. Objects is one of the concepts that take a bit of time to accept. Even though everything in PowerShell is an object, we need to remember that objects have been in the realm of the developer for decades. For IT Pros, this is a foreign concept that we must embrace. Besides, objects make your scripting life so much easier.
Over the course of the next 7 blog postings, we will examine the different components of an object and supply you with some simple code examples to help you understand them better.
Object orientated programming was just a buzz word on the horizon when I graduated college with a degree in Computer Science in the late 90’s. I was out in the real world as a Network Administrator as opposed to a programmer for many years. Sure, I wrote some automation programs for myself, but they were not object based. When PowerShell came about, I did not realize that objects were used at first. It was not until a client asked me to stop teaching VBScript and teach PowerShell that I now needed to understand them.
As I started to play around with PowerShell, the light bulbs all of the sudden turned on in my head with regards to objects and this opened a whole new world of powerful, yet simplified coding for me. These next few blob post will hopefully open your eyes as to what objects are and help you make the transition to a much better place.
In the most basic sense, an object represents something. This could be a user account, a mailbox, a web page, or even a representation of the fan running on your device. If you ask PowerShell to list all of the volumes on your device, each one will have the same members. These members are properties, methods, and events. We will describe each in more detail later on. Take a look at this output.
PS C:\> Get-Volume
DriveLetter FileSystemLabel FileSystem DriveType HealthStatus OperationalStatus SizeRemaining Size
----------- --------------- ---------- --------- ------------ ----------------- ------------- ----
NTFS Fixed Healthy OK 99.03 MB 450 MB
NTFS Fixed Healthy OK 321.21 MB 350 MB
E DATA NTFS Fixed Healthy OK 21.73 GB 238.34 GB
D DATA NTFS Fixed Healthy OK 19.99 GB 238.34 GB
WINRETOOLS NTFS Fixed Healthy OK 212.47 MB 492 MB
C OS NTFS Fixed Healthy OK 5.06 GB 108.29 GB
PBR Image NTFS Fixed Healthy OK 243.9 MB 9.03 GB
Notice how each volume all contain the same members. That is they all have a DriveLetter, HealthStatus, Size, etc… If I asked for the user accounts, would they have a SizeRemaining? Doubtful. In order to by a specific type of object, you must have the same members as other objects of the same type. That way if I grab a list of all user objects, I know that all of the objects have a member called GivenName.
The next few articles in this series will describe each member in much further detail.