• Home
  • Insurance
  • Banking
  • Loans
  • Remitance
  • About us
Facebook Twitter Instagram
  • links
Biz Assurance
Subscribe
  • Home
  • Insurance
  • Banking
  • Loans
  • Remitance
  • About us
Biz Assurance
Home»Tech»Select the Values of One Property on all Objects of an Array in PowerShell
Tech

Select the Values of One Property on all Objects of an Array in PowerShell

Alicia CormieBy Alicia CormieNo Comments3 Mins Read
Facebook Twitter WhatsApp
Share
Facebook Twitter LinkedIn WhatsApp

PowerShell is a data-type driven scripting language, meaning that it supports almost all data types, such as integers, variables, floats, or arrays. More specifically, an array is a data type that stores multiple data types, whether it is a string, variable, or integer. The values stored in an array are stored at a specific index. The first value is stored at the “0” index, the second at the “1” index, and so on. These values can then be selected and called with their specific index number.

This article will cover a detailed procedure to resolve the mentioned query.

How to Select the Values of One Property on all Objects of an Array in PowerShell?

These approaches can be utilized to select the values of one property in an array:

  • Select-Object.
  • $array.Property.
  • %{$_.Property}.

Method 1: Use the “Select-Object” Method to Select all Objects of an Array with the Same Property Values

The “Select-Object” cmdlet is used to select the property values on all objects of an array defined by a user. It is specifically designed to select the specific values defined by a user.

Example

For instance, check the below example code:

$Employees = @(
 [pscustomobject]@{Name=‘Johnny’;Gender=‘Male’;Age=’27’}
 [pscustomobject]@{Name=‘Alice’;Gender=‘Female’;Age=’23’}
 [pscustomobject]@{Name=‘Alex’;Gender=‘Female’;Age=’28’}
)

According to the above code:

  • First of all, create an array and assign it to a “$Employees” variable.
  • Inside an array, create three objects. Each object contains three properties which are “Name”, “Gender”, and “Age”.
  • Assign required values to the defined properties:

Now, let’s select the values of one property on all objects of an array using the “Select-Object” cmdlet:

> $Employees | Select-Object -ExpandProperty Gender

Here:

  • Add the array assigned variable “$Employees” and then use the “Pipeline |” to pass the array output to the “Select-Object” cmdlet.
  • After that, specify the “-ExpandProperty” parameter and assign the “Gender” property to output the values stored in it:

The “Gender” property values on all objects of an array have been displayed in the output.

Method 2: Use the “$array.Property” Method to Select all Objects of an Array with the Same Property Values

Using the “$array.Property” method is the easiest and simplest method to select the values of one property. It concatenates the property with itself to the name to get its values.

Example

In this example, we will access the value of the “Name” property of the “$Employees” array:

> $Employees.Name

Method 3: Use the “%{$_.Property}” Method to Select all Objects of an Array with the Same Property Values

Another method to select and get the values of one property is the “%{$_.Property}” cmdlet. This method is also the easiest one and quite similar to the “$array.Property” cmdlet.

Example

For instance, overview the given example:

> $Employees | %{$_.Age}

Here:

  • First of all, specify the variable and then use the “pipeline |” to send its output to the next command “%{$_.Age}” as an input.
  • “%{$_.Age}” will then select the values assigned to the “Age” property:

The values of one property across all objects have been selected successfully.

Conclusion

In PowerShell the values of a property of an array in the object can be selected using various methods. These methods include “Select-Object”, “$array.Property”, or “%{$_.Property}”. All three methods display the values of the property across all objects in the array. This post has elaborated a detailed procedure to resolve the mentioned query.

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Previous ArticlePowerShell Syntax for Send-Mailmessage – Email to Multiple Recipients
Next Article Pandas Add Column with Default Values

Related Posts

How to Fix “Not connected – No connections are available” Error in Windows

How to Fix “Blurry Font Problem” in Windows 10

How to Fix “Can’t create new folder” in Windows 10

Add A Comment

Leave A Reply Cancel Reply

Grimes Explained Why She’s ‘Purposefully’ Trying To Make Herself ‘Infinitely Less Accessible’

It Appears That Netflix May Have Edited One Of Chris Rock’s Jokes About Will Smith In His Comedy Special

There’s A Rumor That Cardi B And Megan Thee Stallion Want To Remake A Cult-Classic Halle Berry Movie And Cardi Addressed It

The Biggest Takeaways From ‘Bel-Air’ Season 2, Episode 3

  • Homepage
  • Sitemap
© 2023 Biz Assurance - Designed by Curtiex Ventures.

Type above and press Enter to search. Press Esc to cancel.