Need to extract value of attribute from xml from Command promt [closed]

There are literally tons of sample scripts, and docs all over the web explaining and showing how to deal with this use case…

Example: https://www.red-gate.com/simple-talk/sysadmin/powershell/powershell-data-basics-xml

… including discussions on this site.

Though the XML snippet is not complete and you should really provide a more complete one, one can assume what it would look like.

Outside of that, you are not showing what you’ve really tried, meaning your full code effort, and you should for us to really be able to guide you.

Nonetheless, PowerShell has built-in XML cmdlets to deal with XML files …

Get-Command -Name '*xml*' | ft -a

CommandType Name              Version Source
----------- ----              ------- ------
Cmdlet      ConvertTo-Xml     3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet      Convert-XMLtoJSON 5.0.0.1 Sorlov.PowerShell
Cmdlet      Export-Clixml     3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet      Import-Clixml     3.1.0.0 Microsoft.PowerShell.Utility

# Get examples of how to use Select-Xml
Get-Help -Name Select-Xml -Examples

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-xml?view=powershell-6

($MyXml | Select-Xml -XPath "//House").Node.id

# Results
R91958E89-39D6-467C-A149-1674CA7A6D6E

… or you can use the .Net name space for them and cherry pick the data you are after.

[xml]$MyXml = @"
<Homes>
<Home>
    <House id="R91958E89-39D6-467C-A149-1674CA7A6D6E" Address="XYZ" MEMBER="10"/>
</Home>    
</Homes>
"@ 

# Get the house id
$MyXml.Homes.Home.House.id

# Or 
$MyXml.SelectSingleNode('//House').id

# Results for both commands
R91958E89-39D6-467C-A149-1674CA7A6D6E

Leave a Comment