Trying to extract value of a variable from xml in powershell

Hi Everyone,

I am trying to store a variable in XML but I am unable to extract it’s value while running the code. Please help.

$SerialNumber = 'C02KIGKDHDKCJ'
 $Body = @"
<computer>
    <general>
        <name>$($SerialNumber)</name>
    </general>
</computer>
"@ 

Declare the variable type as [xml] and use dot notation to access the value:

$SerialNumber = 'C02KIGKDHDKCJ'
[xml]$Body = @"
<computer>
    <general>
        <name>$($SerialNumber)</name>
    </general>
</computer>
"@ 

$Body.Computer.General.Name
3 Likes

Thank You @matt-bloomfield.