Take the 2-minute tour ×

Here is exam XML..

        <title>a book</title>
        <title>b book</title>
        <title>c book</title>

I want to find a list of this writer is Tom. So my script is

[xml]$books = Get-Content C:\Users\Administrator\Documents\books.xml
$books.book| %{$_.book.writer -match 'Tom'}

But this script show only writer...


I want to see like this

title      writer
a book     Tom
c book     Tom

help me plz.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

You probably want to use ? , a.k.a. Where-Object instead of %:

$books.book| ?{$_.book.writer -match 'Tom'}

This is a filter on the previous pipeline element, restricting what comes out of it, while with ForEach-Object you're projecting, i.e. creating a new sequence of objects.

Your code and output don't make sense, though. First of all, $books shouldn't have a book property but instead books. You should have seen


instead of writer objects, unless there are multiple writer elements per book.

share|improve this answer
wow! Thank you very much. –  user1788012 Aug 30 '13 at 7:47
add comment

Your Answer


