Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the following possible in PowerShell: "Select-Object <Property>.<SubProperty>"?

The scenario: I'm using Select-Object to access properties of a piped object, and one of those properties is itself an object. Let's call it PropertyObject. I want to access a property of that PropertyObject, say Property1. Is there any nice and clean way of accessing Property1, along the lines of:

...| select-object PropertyObject.Property1

While experimenting I can only get it to work if I do something like:

...| select-object {$_.PropertyObject.Property1}

and if I want to display it with a decent column name it gets even messier:

...| select-object @{Name="Property1"; Expression={$_.PropertyObject.Property1}}

Given how clean and concise PowerShell is in general, I can't help thinking I'm missing something and there should be a cleaner way of accessing a property of a property. Is there?

EDIT: As requested by Matt, here is the concrete example:

I'm reading an XML file, Books3.xml:

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date inprint="false">2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
      <publisher>
        <name>Simon and Schuster</name>
        <country>USA</country>
        <city>New York</city>
      </publisher>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date inprint="true">2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
      <publisher>
        <name>HarperCollins</name>
        <country>USA</country>
        <city>New York</city>
      </publisher>
   </book>
   <book id="bk103">
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date inprint="false">2000-11-17</publish_date>
      <description>After the collapse of a nanotechnology 
      society in England, the young survivors lay the 
      foundation for a new society.</description>
      <publisher>
        <name>Macmillan</name>
        <country>United Kingdom</country>
        <city>London</city>
      </publisher>
   </book>
</catalog>

Code to load XML into XmlDocument:

$filePath = "C:\Temp\books3.xml"
$xmlDoc = new-object xml
$xmlDoc.load($filePath)

Attempting to read details for each book:

$xmlDoc.catalog.book | select author, title, publisher.name

Result:

author                     title                      publisher.name
------                     -----                      --------------
Gambardella, Matthew       XML Developer's Guide
Ralls, Kim                 Midnight Rain
Corets, Eva                Maeve Ascendant
like image 344
Simon Tewsi Avatar asked Apr 12 '15 22:04

Simon Tewsi


People also ask

How do I get the properties of an object in PowerShell?

Object properties To get the properties of an object, use the Get-Member cmdlet. For example, to get the properties of a FileInfo object, use the Get-ChildItem cmdlet to get the FileInfo object that represents a file.

What PowerShell command selects specific properties from an object?

Description. The Select-Object cmdlet selects specified properties of an object or set of objects. It can also select unique objects, a specified number of objects, or objects in a specified position in an array. To select objects from a collection, use the First, Last, Unique, Skip, and Index parameters.

Can you create custom properties in PowerShell?

With the help of the Select-Object cmdlet, you can add a calculated property to the output of a PowerShell command. This allows you to enrich the displayed information.


1 Answers

You can use calculated properties. A short form would allow for this:

$xmlDoc.catalog.book | select author, title, {$_.publisher.name}

If the property names are important you need to add them in.

$xmlDoc.catalog.book | select author, title, @{N="PublisherName";E={$_.publisher.name}}

Where N is short for name and E is short for expression.

like image 77
khaledh Avatar answered Sep 29 '22 11:09

khaledh