Handling XML in Powershell

In Powershell, it is very easy to handle XML data as compare to C#. This is because Powershell is not statically type and the property access can be determined at runtime.

The following example shows how to change the value in an AppSetting node:

$f = "$PSScriptRoot\web.config"

# read xml file - the most import is "[xml]" type cast in front of $xml variable.
[xml]$xml = Get-Content $f

# show the nodes in "appSettings"
$xml.configuration.appSettings.add | Format-Table -AutoSize

# change the appSetting
$xml.configuration.appSettings.add[1].value = "1"

# save changes to file
$xml.Save($f)

Comments

Popular posts from this blog

Scheduled task in Windows Task Scheduler - part 3

Create scheduled task in Windows Task Scheduler

Get all website entries in IIS