Print Object in PowerShell

Print Object in PowerShell

Print Object’s Properties in Powershell

In PowerShell, an object is a data structure that contains properties and methods. An object is essentially an instance of a .NET class and can be created using the New-Object cmdlet. PowerShell objects are used extensively in PowerShell scripting.

For example, many cmdlets return objects as output, which can be used as input for other cmdlets or manipulated using PowerShell’s object-oriented features.

PowerShell objects can be easily extended and customized by adding new properties and methods to existing objects or creating new objects based on existing object types.

Let’s create an object to print its properties as follows:

A new object $object is defined with three properties Name, Age, and City. Now, let’s move forward and display the properties by using different methods.

Using Get-Member Cmdlet

Use the Get-Member cmdlet to view the properties of an object.

We used Get-Member cmdlet with the NoteProperty member type to view all the properties of the $object. If you want to view the properties and methods of $object, use All instead of NoteProperty.

The Get-Member cmdlet can accept various values for the -MemberType option that you can find here while All is the default value.

Using Select-Object Cmdlet

Use the Select-Object cmdlet to display specific properties of an object.

Here, we used the Select-Object cmdlet, which is used to choose the object properties or an object or set of objects.

We can also use it to select objects in the given position in an array, unique objects, or a particular number of objects. We can also use the Select-Object with the -ExpandProperty parameter to display the value of the specified property.

Using Format-Table Cmdlet

Use the Format-Table cmdlet to display the properties of an object in a table format.

The Format-Table was used to display the selected properties in a tabular format.

Using Dot Notation

Use the Dot notation to access the properties of an object directly.

Dot notation is helpful if you know the property name and want to retrieve its value.

Until now, we have learned different ways to display the properties of one object; what if we have to show specific or all properties of multiple objects? Let’s see how we can do that below.

Using ForEach-Object Cmdlet

To print the properties of different objects in PowerShell:

  • Create two objects.
  • Make an array of the objects created in the previous step.
  • Use ForEach-Object to print specified properties of all objects in the array created in step two.

First, we created two objects named $object1 and $object2; how to create an object? We have learned it already at the start of the article. Next, we used the array operator represented with @() to create an array of objects and stored it in the $objects variable that we used with ForEach-Object cmdlet to iterate over all objects in the array and print Name property of them. Here, $_ represents the current object.

Each of these methods has advantages and limitations, and the best method depends on the specific use case. If you want to print the entire object then you just write as $YourObjectName and hit Enter.

That’s all about how to print object in PowerShell.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *