Understanding Objects In PowerShell

Objects are everywhere in PowerShell. Everything is an object! Needless to say, it’s important to have a basic understanding of objects. In this video, Adam will go over properties and methods on objects and will show you how to create your own objects with the New-Object cmdlet and the pscustomobject type accelerator.
In this snip, we're going to talk about understanding objects in PowerShell, now objects are an extremely long and complex. Subject we can go on and on about talking about objects in powershell, so we need to limit this down a little bit so in this snip. I'm going to just talk about inspecting existing objects and creating some custom objects. So we're going to scope it down quite a bit so maybe some future snips. We could be able to go into more in depth on object because objects literally. Represent everything in powershell. Alright so to demonstrate objects so let's fist just inspect some object properties, so an object has properties and methods. So to do that. I would just go ahead and create a very simple object, which you may think that well. A string is not an object in some languages not. But in powerShell everything is an object so we have an object here and it looks like when I just do dollar color. Display it onto the terminal console here. It looks simple. There's nothing to it where there's no properties no methods no nothing. Really so let's inspect this a little bit deeper by default powerShell doesn't return all the properties. It depends on some special formatting rules in the background. So to see what properties this has. And we you selected object dash property, then defined the star which will then remove this negates all of the formatting in the background. And then just only shows us the properties in the methods. But it looks like here that it only has one property. So you may think object normally has lots of different properties in powerShell, however, we can look at this, this one property by dot notation, so dot notation is simply Appending, a dot to the end and then specifying the. Property name or the method. So select object is not a great way to see all the properties so there's a lot of different circumstances that are little, too complicated to get into here, but great way that you should get used to finding all the object in property is to use the get member command. So the get member command notice here that it returns a lot more than just length the get member command is the best command, they can use to do exploration in powershell, so notice that with get member. We see all the methods. All the properties. All the everything and notice that string only does have this single length property. There's lots of different methods on the string. Now each properties different integers arrays in any kind of object type. In powershell, there is literally thousands of because it uses the NET Framework, but get member is a really great way to investigate all the properties and methods that are on an object. So that's how you were you reference properties on an object using the dot notation. We use the dot notation as well for methods. But a method is different than a property because it's a verb. It's an action, it does something it calls something. In this case, we have our variable here of color, which is a string and then looking at getting member. You can see that remove is one of the methods. So I'm just picking this at random. We can do any of these remove actually removes characters from the string so let's just try it out and see what this so when I run this. Notice here that we just have R D. It removed the letter E because the remove method in it of itself. The first parameter. There 1 represents the place of the position of the character so all the characters started zero. So R is 0, 1 is E and 2 is D and the second one. There is how many characters to actually remove in this case. I just wanted to do one now I could do. I could change this up to really whatever I want at this point. Remove just allows us to remove whatever we want out of here so let me bring this up again here. Run this again all right there, we go is still working, so let's say that I just want to remove R in this case. I can use zero as the placement here and it's going to move R now remove. Is just again as a sample method. We can use a lot of different ones. There's we can start hitting tab and is another way to figure out which what methods are available on this object. We can use anything so this is our methods. The types of methods will depend mostly on the type of. Object that you have. OK, I've been talking about type So what actually are types types are ways that defined different characteristics for different types of classes and objects so there's a few of the common ones are the string type so the string type is what we've been playing with before it just simple string surrounded by quotes and notice that here with get member. We have the type name of system dot string. Now, if we just use the one here without the quotes. Now you can see that we have a different set of methods so each type has different methods associated with it and now we have the type of system dot Int 32 or just an integer shorthand and then here's another example. We can use Boolean value so notice now that we have system dot boolean. Everything is system that is the name space and then we have the type there, of Boolean so there's a lots of different types. Everything is an object object has a type so there's a lot of different ones. Another thing I wanted to quickly show you was I can use a different way to get the type other than getting member notice that was having to scroll up and down for all this stuff. Maybe I just want to know the type. Every object has a get type method on it, which then outputs an object and I can reference. The name to it, so notice that I can quickly get int 32, there instead of having to scroll down there's another way to get the type of a particular object. Alright next, I would like to show you how to create your own objects in the old days. We used to have to do this. The hard way. But now we can use the PS custom object type accelerator. We would use the new object command specify a type name of PS object so notice that now. I've actually created an object. There is an object in memory that I can play with, but notice there is no there's no properties. There is nothing to this thing at the moment, it's just very, very bare bones. And the type name is system management automation, PS custom objects. So this is the way that you would create these custom objects. Now I want to add a property. There's different types. We're not going to go into all the different member types. There is this is about the easiest way to do this we use member type note property. This is the most common that you'll use the name of the property in this case. I'm using OS build and OS version and then the value. You can put the value to put in some random stuff. OK, when I run this. Now, when I look at object now you can see that we have all of the different ones that I had created we have the old one that OSD that I did on accident there. We have OS build an OS version with the values of it, so that's how we can create an object that was the old way to do it. The new way to do it. We can use the PS custom object type accelerator. It's called the type accelerator because we don't have to go through all the hassle. We did before. This allows us. The PS custom object type. accelerator it allows us to simply define the hashtable here. So notice that we have a hash table if I bring this in. That's a perfectly acceptable construct how it is. But if you put the PS custom object in front of that, then that actually transformed that hash table into a PS custom object type and I will go ahead and save this to a variable so that we know. What's going on here so we can play with a little bit. So now now notice that it has a OS Build and OS version exactly the same thing as before, and then if you don't believe me. We can go into get member here and then you can see the properties of member type, which is no properties and we can see that is a PS Custom object and just like before just by looking at one of those native object types. We can use dot notation here and look at the various. Properties and see that we can reference each of those with our dot notation so that was a very, very brief introduction to objects in powershell like I said, I hope that I'm able to do some more of these in the future. But for now, that should get you started understanding the basics of objects in powershell.