Powershell XPATH seems to be not working

I am using Wifi profile xml files as an example.

In the below code if i use xpath as $XPath = "//*" I get the list of all nodes in the xml.

But if I specify specific node and all nodes under it with $XPath = "//WLANProfile//*", I get nothing.

Is there something wrong I am doing or does Powershell xpath work differently.

$path = "c:\something\wifi.xml"

$XPath = "//WLANProfile//*"

$xmlContent = [xml](Get-Content $path)

#This does not work unless $XPath is changed to "//*"
Select-Xml -Xml $xmlContent -XPath $XPath | Select-Object -ExpandProperty Node

#Just testing another way and same here only works when $XPath is equal to "//*"
$xmlContent.SelectNodes("//*")

XML

<?xml version="1.0"?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
	<name>Guest</name>
	<SSIDConfig>
		<SSID>
			<hex>436170552D477485693</hex>
			<name>dd</name>
		</SSID>
	</SSIDConfig>
	<connectionType>ESS</connectionType>
	<connectionMode>manual</connectionMode>
	<MSM>
		<security>
			<authEncryption>
				<authentication>WPA2PSK</authentication>
				<encryption>AES</encryption>
				<useOneX>false</useOneX>
			</authEncryption>
			<sharedKey>
				<keyType>passPhrase</keyType>
				<protected>true</protected>
				<keyMaterial>secretstring</keyMaterial>
			</sharedKey>
		</security>
	</MSM>
	<MacRandomization xmlns="http://www.microsoft.com/networking/WLAN/profile/v3">
		<enableRandomization>false</enableRandomization>
	</MacRandomization>
</WLANProfile>

Never really used xPath in the many times I’ve done XML parsing, I’ve always used dot notation:

[xml]$xml = Get-Content -Path C:\Scripts\wifi.xml
$xml.WLANProfile.connectionMode
$xml.WLANProfile.MSM.security.authEncryption.authentication

This XML looks straight forward, so is there a reason that you are attempting to leverage XPath?

@rob-simmers no special reason. I don’t work a lot with xml files and google pointed me to xpath. I was thinking this will result in spiting out all the nodes in xml and can be a template for any other similar xml file.

I have come this close

This seems to be caused by namespace as removing the line ‘xmlns=“http://www.microsoft.com/networking/WLAN/profile/v1”’ does make it work.

And I tried using the -Namespace as below and it doesn’t work

Select-Xml '//WLANProfile//*' -Namespace @{'xml'='http://www.microsoft.com/networking/WLAN/profile/v1'} -xml $xmlContent

when namespace is manually removed from file
Node Path Pattern
---- ---- -------
name InputStream //WLANProfile//*
SSIDConfig InputStream //WLANProfile//*
SSID InputStream //WLANProfile//*
hex InputStream //WLANProfile//*
name InputStream //WLANProfile//*
connectionType InputStream //WLANProfile//*
connectionMode InputStream //WLANProfile//*
MSM InputStream //WLANProfile//*
security InputStream //WLANProfile//*
authEncryption InputStream //WLANProfile//*
authentication InputStream //WLANProfile//*
encryption InputStream //WLANProfile//*
useOneX InputStream //WLANProfile//*
sharedKey InputStream //WLANProfile//*
keyType InputStream //WLANProfile//*
protected InputStream //WLANProfile//*
keyMaterial InputStream //WLANProfile//*
MacRandomization InputStream //WLANProfile//*
enableRandomization InputStream //WLANProfile//*