Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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:

$object = New-Object PSObject -Property @{
    Name = "John"
    Age = 35
    City = "New York"
}

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.

$object | Get-Member -MemberType NoteProperty
TypeName: System.Management.Automation.PSCustomObject
Name MemberType   Definition
---- ----------   ----------
Age  NoteProperty int Age=35
City NoteProperty string City=New York
Name NoteProperty string Name=John

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.

$object | Select-Object Name, Age
Name Age     
---- ---  
John  35

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.

$object | Select-Object -ExpandProperty City
New York

Using Format-Table Cmdlet

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

$object | Format-Table -Property Name, Age, City
Name   Age    City    
----   ---    ----    
John   35    New York

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.

$object.Name
John

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.
$object1 = New-Object PSObject -Property @{
    Name = "John"
    Age = 35
    City = "New York"
}
$object2 = New-Object PSObject -Property @{
    Name = "Jim"
    Age = 40
    City = "Washington"
}
$objects = @($object1, $object2)
$objects | ForEach-Object { $_.Name}
John
Jim

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.



This post first appeared on How To Learn Java Programming, please read the originial post: here

Share the post

Print Object in PowerShell

×

Subscribe to How To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×