Using SelectSingleNode in PowerShell with XML Namespace (Azure VNetConfig)

So, been doing a lot of work in Azure lately and having to solve problems I have not had to deal with before.  As you know the Network Configuration for Azure if not done through the portal is done through an XML file. Below is an example of an XML file exported from Azure with the command get-azurevnetconfig –ExportToFile c:\test\network.xml

<?xml version="1.0" encoding="utf-8"?>
<NetworkConfiguration  >
    <Dns />
      <VirtualNetworkSite name="test" Location="East US">
          <Subnet name="Subnet-1">

So let’s say you want to update the AddressPrefix for the Virtual Network Site test. So you load the xml file into a variable. [xml] $azurenetconfig = get-content -path "$PWD\network.xml"

With PowerShell and XML you should be able to run the following command and get data back. But you get no data back… why is that?



By reading MSDN you can see that when dealing with XML that has a namespace you must specify the namespace.

So for this command to work we must define the namespace, that is done in the commands below.

$ns = New-Object System.Xml.XmlNamespaceManager($network.NameTable)
$ns.AddNamespace("ns", $network.DocumentElement.NamespaceURI)

Now we need to run the same SelectSingleNode method but with the namespace defined.

$network.SelectSingleNode("//ns:VirtualNetworkSite[@name='test']", $ns)

Now you can see we actually get data back.  Hope this helps someone that might be struggling with this or leveraging any XML with a namespace


Comments (5)

  1. Anonymous says:

    Thanks for the article

  2. Nirjhar says:

    Thanks a lot, This article saved my day 🙂

  3. Sam says:

    Good Article you have posted. Thanks for sharing.

  4. Stig Hemmer says:


  5. Super article, saved lot of efforts

Skip to main content