SuperJMN
SuperJMN

Reputation: 14002

PowerShell to update XML element (inner text)

Given this XML:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build">
  <PropertyGroup>
    <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
  </PropertyGroup>
</Project>

I would like to change the "v.4.6.1" by "v4.7.1" using PowerShell.

Currently, I load the XML above with this:

$content = [xml](Get-Content $path.FullName)

Upvotes: 1

Views: 926

Answers (1)

mklement0
mklement0

Reputation: 440142

$content.Project.PropertyGroup.TargetFrameworkVersion = 'v4.7.1'

This relies on PowerShell conveniently exposing an XML document's DOM by decorating the [System.Xml.XmlNode] instances that make up the object hierarchy with properties that reflect a node's XML attributes and child elements, which enables regular dot notation.

Upvotes: 4

Related Questions