Select-Xml

Applies To: Windows PowerShell 2.0

Finds text in an XML string or document.

Syntax

Select-XML [-Xpath] <string> [-Xml] <XmlNode[]> [-Namespace <hashtable>] [<CommonParameters>]

Select-XML [-Xpath] <string> -Content <string[]> [-Namespace <hashtable>] [<CommonParameters>]

Select-XML [-Xpath] <string> [-Path] <string[]> [-Namespace <hashtable>] [<CommonParameters>]

Description

The Select-XML cmdlet lets you use XPath queries to search for text in XML strings and documents. Enter an XPath query, and use the Content, Path, or Xml parameter to specify the XML to be searched.

Parameters

-Content <string[]>

Specifies a string that contains the XML to search. You can also pipe strings to Select-XML.

Required?

true

Position?

named

Default Value

None

Accept Pipeline Input?

true (ByValue)

Accept Wildcard Characters?

false

-Namespace <hashtable>

Specifies a hash table of the namespaces used in the XML. Use the format @{<namespaceName> = <namespaceValue>}.

Required?

false

Position?

named

Default Value

None

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Path <string[]>

Specifies the path and file names of the XML files to search. Wildcards are permitted.

Required?

true

Position?

2

Default Value

None

Accept Pipeline Input?

true (ByPropertyName)

Accept Wildcard Characters?

true

-Xml <XmlNode[]>

Specifies one or more XML nodes.

An XML document will be processed as a collection of XML nodes. If you pipe an XML document to Select-XML, each document node will be searched separately as it comes through the pipeline.

Required?

true

Position?

2

Default Value

None

Accept Pipeline Input?

true (ByValue, ByPropertyName)

Accept Wildcard Characters?

false

-Xpath <string>

Specifies an XPath search query. The query language is case-sensitive. This parameter is required.

Required?

true

Position?

1

Default Value

None

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

<CommonParameters>

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -OutBuffer, -OutVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

Inputs and Outputs

The input type is the type of the objects that you can pipe to the cmdlet. The return type is the type of the objects that the cmdlet returns.

Inputs

System.String or System.Xml.XmlNode

You can pipe a path or XML node to Select-XML.

Outputs

Microsoft.PowerShell.Commands.SelectXmlInfo

Notes

XPath is a standard language that is designed to identify parts of an XML document. For more information about the XPath language, see the "Selection Filters" section of the "Event Selection" topic in the MSDN (Microsoft Developer Network) library at https://go.microsoft.com/fwlink/?LinkId=143608. And, see "XPath Reference" in the MSDN library at https://go.microsoft.com/fwlink/?LinkId=143609.

Example 1

C:\PS>$path = "$pshome\Types.ps1xml"

C:\PS> $xpath = "/Types/Type/Members/AliasProperty"

C:\PS> select-xml -path $path -xpath $xpath | select-object -expandProperty Node

Name                 ReferencedMemberName    
----                 --------------------    
Count                Length                  
Name                 Key                     
Name                 ServiceName             
RequiredServices     ServicesDependedOn      
ProcessName          Name                    
Handles              Handlecount             
VM                   VirtualSize             
WS                   WorkingSetSize          
Name                 ProcessName             
Handles              Handlecount             
VM                   VirtualMemorySize       
WS                   WorkingSet              
PM                   PagedMemorySize         
NPM                  NonpagedSystemMemorySize
Name                 __Class                 
Namespace            ModuleName

Description
-----------
This example searches the Types.ps1xml file for child items of the AliasProperty node. (For information about this file, see about_Types.ps1xml.)

Select-XML returns a SelectXMLInfo object that has a Node property, which is a System.Xml.XmlElement object. The Node property has an InnerXML property, which contains the actual XML that is retrieved. It also has properties for each of the two XML elements in the node, Name and ReferencedMemberName.

In this example, a pipeline operator (|) sends the SelectXMLInfo objects that Select-XML returns to a Select-Object command. The ExpandProperty parameter displays the properties of the Node property of the SelectXMLInfo object.





Example 2

C:\PS>select-xml -path test*.xml, help.xml -xpath "/Tests/Test[1]/Name"

Description
-----------
This command uses Select-XML to search in several XML files.





Example 3

C:\PS>[xml]$Types = get-content "$env:windir\System32\WindowsPowerShell\v1.0\Types.ps1xml"

C:\PS> select-xml -xml $Types -xpath "//MethodName"

Description
-----------
This example shows how to pipe an XML document to Select-XML.





Example 4

C:\PS>$namespace = @{command="https://schemas.microsoft.com/maml/dev/command/2004/10"; maml="https://schemas.microsoft.com/maml/2004/10"; dev="https://schemas.microsoft.com/maml/dev/2004/10"}

C:\PS> $path = "$pshome\en-us\*dll-Help.xml"

C:\PS> $xml = select-xml -path $path -namespace $namespace -xpath "//command:name"

C:\PS> $xml | format-table @{Label="Name"; Expression= {($_.node.innerxml).trim()}}, Path -auto

Name                    Path                                                                                                   
----                    ----                                                                                                   
Export-Counter          C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Diagnostics.dll-Help.xml
Get-Counter             C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Diagnostics.dll-Help.xml
Get-WinEvent            C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Diagnostics.dll-Help.xml
Import-Counter          C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Diagnostics.dll-Help.xml
Add-Computer            C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Management.dll-Help.xml 
Add-Content             C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Management.dll-Help.xml 
Checkpoint-Computer     C:\Windows\system32\WindowsPowerShell\v1.0\en-us\Microsoft.PowerShell.Commands.Management.dll-Help.xml 
...

Description
-----------
This example shows how to use the Select-XML cmdlet to search the Windows PowerShell XML-based cmdlet help files. 

The first command creates a hash table that represents the XML namespace and saves it in the $namespace variable.

The second command saves the path to the help files in the $path variable.

The third command uses Select-Xml to search the XML for cmdlet names by finding Command:Name tags anywhere in the files. It saves the results in the $xml variable.

Select-XML returns a SelectXMLInfo object that has a Node property, which is a System.Xml.XmlElement object. The Node property has an InnerXML property, which contains the actual XML that is retrieved. 

The fourth command sends the XML in the $xml variable to the Format-Table cmdlet. The Format-Table command uses a calculated property to get the Node.InnerXML property of each object in $xml, trim the white space before and after the text, and display it in the table, along with the path to the source file.





Example 5

C:\PS>$xml = @"
<?xml version="1.0" encoding="utf-8"?>
 <Book>
   <projects>
     <project name="Book1" date="2009-01-20">
       <editions>
          <edition language="English">En.Book1.com</edition>
          <edition language="German">Ge.Book1.Com</edition>
          <edition language="French">Fr.Book1.com</edition>
          <edition language="Polish">Pl.Book1.com</edition>
       </editions>
     </project>
   </projects>
 </Book>
"@

C:\PS> select-xml -content $xml -xpath "//edition" | foreach {$_.node.InnerXML}
En.Book1.com
Ge.Book1.Com
Fr.Book1.com
Pl.Book1.com


C:\PS> $xml | select-xml -xpath "//edition" | foreach {$_.node.InnerXML}    

En.Book1.com
Ge.Book1.Com
Fr.Book1.com
Pl.Book1.com

Description
-----------
This example shows two different ways to send XML to the Select-XML cmdlet.

The first command saves the here-string in the $xml variable. (For more information about here-strings, see about_Quoting_Rules.)

The second command uses the Content parameter to specify the XML in the $xml variable. 

The third command is equivalent to the second. It uses a pipeline operator (|) to send the XML in the $xml variable to the Select-XML cmdlet.





See Also

Concepts

ConvertTo-XML