1

I am trying to fetch an xml attribute value using powershell but not sure how to read the attributes.

Here is my sample xml file with name myfile.xml

<MyFile>
<Parameter name ="internet" enabled ="true" />
</MyFile>

Please suggest me how to fetch the value of attribute enabled using powershell.

bijoy26
  • 133
  • 9
  • possible dupplicate https://stackoverflow.com/questions/12212452/how-to-fetch-an-attribute-value-from-xml-using-powershell – Akki Jun 11 '21 at 17:50

1 Answers1

1

Here's a simple solution.

You read from the file, cast it as XML object and then reference properties via dot notation.

$XMLPath = "ENTER_YOUR_FILE_PATH"

# Read the file content, cast it as an XML object and store in variable
$xmlfile = [xml](Get-Content $XMLPath)

# Access particular attribute value
$xmlfile.MyFile.Parameter.enabled

Output

true

Additional methods are listed here.

bijoy26
  • 133
  • 9
  • Thank you for this answer. Also can you suggest for below Now i want to get the value of "enabled" based upon attribute "name" – Hari Krishna Jun 11 '21 at 17:50
  • In order to access conditional attribute values by iteration, you can use **Where-Object** and **ForEach-Object**. For printing the value of _enabled_ for _"internet"_ , the statement goes something like: `$xmlfile.MyFile.Parameter | Where-Object name -eq 'internet' | ForEach-Object { $_.enabled }` – bijoy26 Jun 11 '21 at 18:21