PowerShell: Objects, Properties and Methods
Welcome back to our PowerShell tutorial series! In the previous post, we talked about cmdlets and alias and using pipelines to pass data between cmdlets. In this post, we’ll explore objects, object properties, methods and how to use object properties in pipelines.
In PowerShell, an object is a collection of properties and methods. Properties are pieces of information that describe the object. For example, a file object might have properties like name, size, and creation date. We can use these properties in pipelines to filter or manipulate data. Methods are the activities you can perform on the objects. Every output from a command is an object, and every command you write should output an object. So objects are building blocks of PowerShell.
Properties
As we know an object is made up of properties and methods. Let's check for an object and what are all the properties and methods exist. To get this detail, we can execute the Get-member
cmdlet. Now you can see all the different properties and Methods that exist on the Get-Service
cmdlets. There are other member types also present. But we will focus only on the property and method.
If you want to know about each service's name, status and start type, you can get the same using the object's property.
Get-Service | Select-Object Name, Status, StartType
Another example. Suppose we have a list of files, and we want to filter the list to include only files that are larger than 5MB. Get-ChildItem
cmdlet is going to list out all the contents of the current directory. Now, we can use the length property of the file to filter out the file list that displayed. We can do this using the Where-Object
cmdlet, which filters objects based on a specified condition. Here’s how we can use it:
Get-ChildItem | Where-Object {$_.Length -gt 5MB}
In this example, Get-ChildItem
gets a list of files, and then the pipeline passes each file object to Where-Object
. The $_
variable represents the current object in the pipeline, and we access the Length property using $_.Length
. We compare this to 5MB using the -gt
(greater than) operator.
We can also use object properties to group data in pipelines. For example, suppose we have a list of files, and we want to group them by their file extensions. We can use the Group-Object
cmdlet to do this, like so:
Get-ChildItem | Group-Object Extension
In this example, Get-ChildItem
gets a list of files, and then the pipeline groups the files by their Extension property. The resulting output shows the number of files in each group.
We can also use object properties to sort data in pipelines. We can use the Sort-Object
cmdlet to do this, like so:
Get-ChildItem | Sort-Object FullName
Get-ChildItem | Sort-Object FullName -Descending
After sorting the file name, then we want to display only the file name and their length. So we are using Select-Object
cmdlet to display only the name and length.
Methods
Methods are actions that can be performed on an object. For example, if you have an object that represents a file, you can use the Copy-Item
cmdlet to make a copy of that file.
To access an object's methods, you can use dot notation. For example, if you want to use the Copy-Item
method on an object, you can use the following syntax:
$object.CopyItem(destination)
This will copy the object to the specified destination.
In conclusion, using object properties and methods in pipelines is a powerful way to filter, group, sort, and display data in PowerShell and do some action on the object. By combining object properties and methods with other PowerShell features like cmdlets and pipelines, we can create complex data processing workflows with ease.
That’s all for this post! Stay tuned for the next post!