gpresland
gpresland

Reputation: 1819

PowerShell Core `Select-Xml` Returns Nothing

I have an XML file I want to get value(s) from with PowerShell Core.

Cleaned up example:

  <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

  <PropertyGroup>
    <TargetFrameworks></TargetFrameworks>
    <RootNamespace></RootNamespace>
    <AssemblyName></AssemblyName>
    <Company></Company>
    <Authors></Authors>
    <Description></Description>
    <Copyright></Copyright>
    <AssemblyVersion></AssemblyVersion>
    <FileVersion></FileVersion>
    <Product></Product>
  </PropertyGroup>

  <PropertyGroup>
    <Version>1.0.0</Version>
    <RepositoryUrl></RepositoryUrl>
    <GeneratePackageOnBuild></GeneratePackageOnBuild>
  </PropertyGroup>

</Project>

I want to get the value of the <Version> element, which is 1.0.0.

I run:

pwsh Select-Xml -Path test.csproj -XPath "//Version"

Which returns:

Node    Path                     Pattern
----    ----                     -------
Version D:\Downloads\test.csproj //Version

When I try to get the value as per Microsoft's docs, with:

pwsh Select-Xml -Path test.csproj -XPath "//Version" | Select-Object -ExpandProperty Node

I get the following error:

Select-Object : Property "Node" cannot be found.
At line:1 char:56
+ ... h test.csproj -XPath "//Version" | Select-Object -ExpandProperty Node
+                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:PSObject) [Select-Object], PSArgumentException
+ FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand

Select-Object : Property "Node" cannot be found.
At line:1 char:56
+ ... h test.csproj -XPath "//Version" | Select-Object -ExpandProperty Node
+                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (Node    Path                     Pattern:PSObject) [Select-Object], PSArgumentException
+ FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand

Select-Object : Property "Node" cannot be found.
At line:1 char:56
+ ... h test.csproj -XPath "//Version" | Select-Object -ExpandProperty Node
+                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (----    ----                     -------:PSObject) [Select-Object], PSArgumentException
+ FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand

Select-Object : Property "Node" cannot be found.
At line:1 char:56
+ ... h test.csproj -XPath "//Version" | Select-Object -ExpandProperty Node
+                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (Version D:\Download\u2026st.csproj //Version:PSObject) [Select-Object], PSArgumentException
+ FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand

Select-Object : Property "Node" cannot be found.
At line:1 char:56
+ ... h test.csproj -XPath "//Version" | Select-Object -ExpandProperty Node
+                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:PSObject) [Select-Object], PSArgumentException
+ FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand

How do I get this XML element's value?

Upvotes: 0

Views: 428

Answers (1)

js2010
js2010

Reputation: 27546

Pwsh defaults to -file instead of -command. There's also a quoting issue with the pipe symbol. This works, assuming you're running it from cmd:

pwsh -c "Select-Xml //Version test.csproj | Select -Expand Node"

#text
-----
1.0.0

Or

pwsh -c (Select-Xml //Version test.csproj).node

If this is run from unix or other versions of powershell, there's different issues.

Upvotes: 0

Related Questions