Before You Start PowerShell
December 8, 2014PowerShell is an amazing tool that has limitless potential for Administrators, Engineers and Architects to automate routine tasks or do reporting on things their system management applications aren’t built for. Whenever there is a task to be done on multiple systems and it might need to be done more than once, I find myself reaching for this valuable tool.
The problem with PowerShell, just like a programming language is that it can be intimidating to get started. This post is to give you a basic understanding of what you’ll be getting into before you start running PowerShell cmdlets.
Objects
Dictionary.com defines an object as: “a thing, person, or matter to which thought or action is directed“.
I spy a computer monitor. You’re computer monitor is an object right? Assuming we’re not talking about scripting for a second, no one would argue with me that your computer monitor is not an object. If you look around you, I’m sure you can find tons of objects just sitting around but I chose to use a computer monitor because there is a decent chance you’re looking at one right now and everyone can relate to this.
Let’s look at an object in a PowerShell context. Some examples of an object for scripting could be:
- A file
- A directory
- a service
- a device
If I open up a PowerShell window, I can take this object and put it into a variable for future use. Let’s try it with a file called “Hollow.bmp” which is just a bitmap picture of the theITHollow.com logo.
I’ve created a variable called “variable” and put in it the object “Hollow.bmp”
Simple so far right?
Properties
Now that we have an object defined in a variable, let’s talk about properties. A property is a distinctive attribute or quality about an object. Going back to the analogy of your computer monitor being an object, let’s think about what a property of your computer monitor would be. Just to name a few of them, think of Screen Resolution, Bezel Color, Refresh Rate and Size. Everyone’s computer monitor should have these types of properties associated with them.
Just like our computer monitors, PowerShell Objects also have properties. Let’s look at our variable again where the bitmap file “Hollow.bmp” was loaded into it.
Here, I’ve run two commands. The first one is just to recall the $variable. When I type in my object, a list of default properties will be returned. In this case you can see that Directory, Last Write Time and Name are a few attributes that can be used to describe the object in our variable.
The second command was to list all of the properties of the object in our variable. As you can see there are many more properties of the object that can be referenced besides the default properties of the object.
Methods
Methods are things that you can do with an object. This could be modifying a property or returning a value to you. Back to the Computer Monitor object example, what kind of methods could you imagine your monitor has? How about PowerOff, PowerOn or RotateScreen? These are all things that can be done to the object.
In our PowerShell script we can list the methods available on our Hollow.bmp file.
Below, I’ve listed the methods that are available on the Hollow.bmp file and you can see they are all things to do to the file. Also, you’ll notice that in the definition you’ll see how the method is typically called.
For instance, the Encrypt method has a definition that looks like “Encrypt()”. The parentheses are used to store additional variables. Since Encrypt doesn’t have anything inside the parentheses, there are no options that can be added to the method. See Below, where I’ve used both the Encrypt and MoveTo methods on the file. See that the encrypt method has no additional paramaters, but the MoveTo method needs a new file location.
Summary
This post isn’t going to make you a PowerShell expert, but understanding how Objects, Methods and Properties all work should give you a good jump start on your scripting. These are just some ideas that are helpful to know going into your scripting endeavors so you’re not just blindly copying scripts off the Internet to get your tasks done.
Excellent post Eric!
I have just jumped into PowerShell head first over the past 2 weeks to get past that intimidation phase, and I really love it. But I still find myself questioning some parts to get a better grasp on how or why it works that way. This post is a nice simple layout of the basics to wrap your head around. Thank you for sharing!
Nigel
Thanks for the feedback.
[…] you Start PowerShell Here is an article about some basic terms in PowerShell that are good to know about and understand before you start […]