# Extract XML Element

**URL:** <https://forums.powershell.org/t/extract-xml-element/14102>\
**Category:** PowerShell Help\
**Created:** [April 6, 2020, 5:54am UTC](https://forums.powershell.org/t/extract-xml-element/14102 "2020-04-06T05:54:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mutzig1](https://avatars.discourse-cdn.com/v4/letter/m/b2d939/32.png) [@mutzig1](https://forums.powershell.org/u/mutzig1)\
**Post date:** [April 6, 2020, 5:54am UTC](https://forums.powershell.org/t/extract-xml-element/14102/1 "2020-04-06T05:54:48Z")

</div>

Hi, I’m new to working with Powershell and would appreciate some help with extracting the element from an XML file and then output this to a txt file using powershell.

```
<?xml version="1.0" encoding="utf-8"?>
<Generic version="1.0.0">
<ValueId>12345678</ValueId>
<ServerIP>127.0.0.1</ServerIP>
</Generic>
```
I've been able to extract the value 12345678 to a file using this powershell;
```
$XML = [XML](Get-Content C:\source.xml)
$XML.Generic.ValueId | Out-File C:\output.txt
```
I'm struggling to get this exact \<ValueId\>12345678\</ValueId\> output to the output.txt Thanks
```
```
```
```

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [April 6, 2020, 7:39am UTC](https://forums.powershell.org/t/extract-xml-element/14102/2 "2020-04-06T07:39:21Z")

</div>

Use SelectSingleNode and if you want the XML specify OuterXML:

```
[xml]$xml = @"
<?xml version="1.0" encoding="utf-8"?>
<Generic version="1.0.0">
<ValueId>12345678</ValueId>
<ServerIP>127.0.0.1</ServerIP>
</Generic>
"@

$xml.SelectSingleNode('//Generic/ValueId').OuterXml
```

---

<div class="post-metadata">

**Author:** ![mutzig1](https://avatars.discourse-cdn.com/v4/letter/m/b2d939/32.png) [@mutzig1](https://forums.powershell.org/u/mutzig1)\
**Post date:** [April 6, 2020, 9:21am UTC](https://forums.powershell.org/t/extract-xml-element/14102/3 "2020-04-06T09:21:43Z")

</div>

Thanks for the quick reply, that works perfectly.

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [April 6, 2020, 10:10am UTC](https://forums.powershell.org/t/extract-xml-element/14102/4 "2020-04-06T10:10:38Z")

</div>

Nice job displaying the xml. Another way with select-xml:

```
(select-xml //ValueId source.xml).node.OuterXml
     
<ValueId>12345678</ValueId>
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:30pm UTC](https://forums.powershell.org/t/extract-xml-element/14102/5 "2024-05-16T20:30:14Z")

</div>


