Powershell XPATH seems to be not working

RT-7199 511 Reputation points
2023-03-21T05:12:51.91+00:00

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>

Thanks

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,468 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 46,561 Reputation points
    2023-03-21T15:03:22.2233333+00:00

    I suspect your problem is with namespaces. If you remove the "xmlns=...." from the WLANProfile element your search works.

    0 comments No comments

  2. Limitless Technology 44,221 Reputation points
    2023-03-21T15:41:16.0433333+00:00

    Hello there,

    Maybe the issue is with the xml namespace. Try using the -namespace to get the correct xpath query.

    Test this by removing the xmlns from a feature.xml file and running your command.

    The below thread discusses the same issue and you can try out some troubleshooting steps from this and see if that helps you to sort the Issue.

    https://learn.microsoft.com/en-us/answers/questions/1116596/need-a-little-help-with-powershell-and-xpath-pleas

    Hope this resolves your Query !!

    --If the reply is helpful, please Upvote and Accept it as an answer--

    0 comments No comments

  3. RT-7199 511 Reputation points
    2023-03-21T22:03:25.86+00:00

    @Limitless Technology and @Rich Matheisen

    Yes 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//*

    I think i am almost there if you help me with that. Thanks

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.