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.

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for this answer. Also can you suggest for below <My File> <Parameter name ="internet" enabled ="true" /> <Parameter name ="wifi" enabled ="true" /> </My File> Now i want to get the value of "enabled" based upon attribute "name"
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 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.