PowerShell commands for Beginners – Part 2

We have seen few fundamental commands in the previous article and now discuss few more commands in this PowerShell Commands for Beginner – Part 2 article that talks about working on the PowerShell object.

What is PowerShell Object:

PowerShell is an object-based scripting language. Most of the time you deal with objects throughout your scripts. Each command that you use throws an object or array of objects as output. Then what is an object? In simple terms, the object is nothing but a representation of something that has some facets to talk about and actions to perform against.

For example, when you talk about a car, you talk about color, model, brand, etc. These are nothing but properties and the actions that you perform i.e. Start, Stop, etc are called methods in the programming world.

PowerShell makes use of .Net framework under the hood and provides object based scripting. You no need to define the classes, but PowerShell uses the .Net libraries to relay upon and provides the objects get instantiated based on the type.

For example:

# Using Strings
$Var = "Welcome to PowerShell"

When you assign a string, then PowerShell will identify the type of the value you provided and instantiates an object of the String class. This is called Dynamic Typing and inherits the properties and methods from the String class which means, you are creating an object that will have all the properties and methods that the string supposed to have. To know the class and type of the variable that we have defined above.

$var.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

As in the above, the object $var is based in the String class that based on System.object.

How do you access the properties and methods of the object.

Use the . (dot) to access the properties and methods of the objects as below.

# To know what properties that the object of String type has, use the command that we discussed previous article.

$Var | Get-member

TypeName: System.String
Name             MemberType            
----             ----------            
Clone            Method                
CompareTo        Method                
Contains         Method
Split            Method                
Trim             Method                
TrimEnd          Method                
TrimStart        Method                
Length           Property 

# Now you can which a property and which a method. Sometime that properties allows to read only and you cannot set.

# Now use the . (dot) to access properties and methods. As per the Get-Member output you got, we have only one property that is Length

PS> $var.Length
21

# Now, lets perform some actions, such as split that allow us to split the string into chunks. Now let us slipt the $var value into words, based a space. Which means a sentance into words

PS > $var.split()
Welcome
to
PowerShell

# As in above we have done an action to break that string which we call it as method and you refer to Get-Member output.

To know more about printing and accessing the properties refer to Interpolation in PowerShell

Hope you find the article useful.

Leave a comment

0 Shares
Share via
Copy link
Powered by Social Snap