Blade1 Blade1 - 1 year ago 62
PowerShell Question

Get element depending on value of attribute within

I have a XML document laid out like below:

<?xml version="1.0" encoding="utf-8"?>
<mailmanager xmlns:xsi="">
<store id="57fb3dc3-8716-4a71-ab5c-d8c76e20640b">
<description>01038 - Wood Wharf - Fire</description> <folder>SharePoint:</folder>
<store id="6873a00e-e49c-4602-af27-49d2900563d3">
<description>01038 - Wood Wharf - Fire</description>
<store id="d5e3af58-bc1d-4e45-a2dc-a9ceed803456">
<description>05515 - IRELAND - Dart Underground   -  DART UNDERGROUND INTERCONNECTOR PROJECT</description> <folder>SharePoint:</folder>

With thousands of

With PowerShell, is it possible to get an entire parent element (i.e. store with everything within) based on the
value? So if
value contains
for example, I get the parent element.

Answer Source

Sure, just select all stores and filter the one which folder child contain a specific value using the Where-Object cmdlet:

[xml]$content = Get-Content 'your_xml'
$ | 
    Where-Object { $_.folder -eq '\\\legal\01038\documents\' }


id          : 6873a00e-e49c-4602-af27-49d2900563d3
type        : msg
description : 01038 - Wood Wharf - Fire
folder      : \\\legal\01038\documents\
fileable    : 0