How can I write Xml.linq in PowerShell?


I got this to work, (uncommenting something from an xml document) using linq in powershell:

[Reflection.Assembly]::LoadWithPartialName("System.Xml.Linq") | Out-Null

$xDoc = [System.Xml.Linq.XDocument]::Load("web.config")
$endpoints = $xDoc.Descendants("client") | foreach { $_.DescendantNodes()}               
$comments = $endpoints | Where-Object { $_.NodeType -eq [System.Xml.XmlNodeType]::Comment -and $_.Value -match "net.tcp://localhost:9876/RaceDayService" }        
$comments | foreach { $_.ReplaceWith([System.Xml.Linq.XElement]::Parse($_.Value)) }


If you were to write PowerShell Modules, you'd create a Manifest file, which would have similar dependencies loaded upon Import-Module MyModule call:

# Comma-separated assemblies that must be loaded prior to importing this module
RequiredAssemblies = @("System.Xml.Linq")

This is the recommended way for those who write modules.