What are Objects in PowerShell?

PowerShell is full of objects. And dealing with different types of objects is something you’ll spend a lot of time doing if you want to get into PowerShell.

But what really are objects in PowerShell? The answer is “pretty much everything”.

Every command you run in PowerShell returns an object, or an array of objects. If you don’t know what an array is, it’s basically a data type that contains a group of objects. You have tons of different types of objects. Don’t think too hard about different object types, just know that every “type” of object is different. I’ll show you how to check what kind of object you’re dealing with.

So, every time you run a command in PowerShell, it returns an object. And that object has an object-type, properties and methods. Properties is something that describes the object, methods are “actions” or code that you can execute against the object.

Time for an example! Let’s create a variable called test and then assign a string to that variable:

 $Test = "This is a string." 

Now if we just run $Test in our Shell, PowerShell returns “This is a string.”.

teststring.png

Now I’m going to show you the most important command of them all:

 Get-Member 

When you pipe something into Get-Member, PowerShell is gonna spit out what type of object you’re dealing with, all the methods, and all the properties of the object.

So let’s pipe our $Test variable into Get-Member and see what we’re dealing with:

 $Test | Get-Member 

testgm

First thing we see is the TypeName. This is basically telling you what type of object you’re dealing with. A System.String object. Or just a String object for short.

Next Get-Member lists all the Methods and Properties of the object.

testgm2.png

So let’s first focus on the Properties of our String Object. As you can see in the very long list above, this object only has one Property: Length. You can invoke properties by writing:

 $Test.Length 

lenght

Methods are invoked the same way, but they end with a ( ) after the method name. Some methods require input inside the ( ).

Let’s try the ToUpper method:

 $Test.ToUpper() 

toupper.png

See what our ToUpper method did to our string?

Now listen, calling a method on a object does not change the object itself. It merely outputs a new modified version of the object. That means if you run $Test again after previously running the ToUpper method, you’ll see that your string remains unchanged.

If your goal was to actually change the object stored in the $Test variable, you would have to declare it again like this:

 $Test = $Test.ToUpper() 

Now your $Test would look a bit different:

touppernew

Using Get-Help and Get-Command from my previous blog post “How to discover PowerShell commands” combined with looking at the Object type, properties and methods using Get-Member should make you a pretty great PowerShell explorer!

This is my first time trying to explain objects in PowerShell in writing. If any of this didn’t make sense, please let me know! I’ll answer any question you might have in the comments.

// Tr4p

2 thoughts on “What are Objects in PowerShell?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.