There are probably many solutions. One quick is this one:
$xml = [xml]"<car>
<germany>
<manufacturer>Opel</manufacturer>
<manufacturer>BMW</manufacturer>
<manufacturer>Benz</manufacturer>
<manufacturer>b</manufacturer>
<manufacturer>a</manufacturer>
<manufacturer>c</manufacturer>
</germany>
<japan>
<manufacturer>Nissan</manufacturer>
<manufacturer>Daihatsu</manufacturer>
</japan></car>"
$g = $xml.car.germany| select-xml 'manufacturer' | Select-Object -expand Node | sort '#text'
$j = $xml.car.japan | select-xml 'manufacturer' | Select-Object -expand Node | sort '#text'
@"
<car>
<germany>
$( ($g | % { $_.OuterXml}) -join "`n`t" )
</germany>
<japan>
$( ($j | % { $_.OuterXml}) -join "`n`t" )
</japan></car>
"@
Note that I used Select-Xml
to extract manufacturers from the xml, because $xml.car.germany.manufacturer
returned just array of strings (you probably had problems with that). I wasn't able to get the original object, even with help of PsBase
property. That's why I used Select-Xml
.