Understanding and using objects in PowerShell

How to use PowerShell objects, how to tease more info and functionality out of them and how objects can be useful in scripting scenarios.

Become An Insider

Sign up now and get FREE access to hundreds of Insider articles, guides, reviews, interviews, blogs, and other premium content. Learn more.

One of the things most people do not realize about PowerShell, at least up front, is that PowerShell is based on the .NET Framework, which means that PowerShell can be considered a programming language. In fact, each response you get from running a cmdlet in PowerShell, no matter how simple or complex that cmdlet may be, is actually a .NET object. It might look like text to you, but it can be programmatically manipulated in ways that Linux and UNIX command line diehards can only dream about.

In this piece I'll focus on using PowerShell objects, how to tease more info and functionality out of them, and how objects can be useful in scripting scenarios.

What is an object?

It would probably help to know what an object is so that you can understand just how useful this capability of PowerShell is.

Objects are essentially known quantities of something that programming languages can use, interact with, perform computations and transformations on, and in general "consume." Technically, an object is simply the programmatic representation of anything. Objects are usually considered as two types of things: Properties, which simply describe attributes of whatever the .NET object is representing, and methods, which describe the types of actions (think verbs, or short instructions) that the .NET object can undertake.

For example, let us consider a car as an example. If we were making a car into a .NET object, then its properties would include its engine, doors, accelerator and brake pedals, steering wheel and headlights. Its methods would include turn engine on, turn engine off, open doors, close doors, press accelerator, release accelerator, turn steering wheel left, turn steering wheel right, turn on headlights, turn off headlights, turn on brights and turn off brights. (That is not an exhaustive list, but it should serve to demonstrate to you that the properties of the car are a description of its components, and the methods of the car describe how you can operate and interact with the properties.)

To continue reading this article register now

Join the discussion
Be the first to comment on this article. Our Commenting Policies