Select-Xml

Finds text in an XML string or document.

Syntax

Select-Xml
      [-XPath] <string>
      [-Xml] <XmlNode[]>
      [-Namespace <hashtable>]
      [<CommonParameters>]
Select-Xml
      [-XPath] <string>
      [-Path] <string[]>
      [-Namespace <hashtable>]
      [<CommonParameters>]
Select-Xml
      [-XPath] <string>
      -LiteralPath <string[]>
      [-Namespace <hashtable>]
      [<CommonParameters>]
Select-Xml
      [-XPath] <string>
      -Content <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.

Examples

Example 1: Select AliasProperty nodes

This example gets the alias properties in the Types.ps1xml. For information about this file, see about_Types.ps1xml.

The first command saves the path to the Types.ps1xml file in the $Path variable.

The second command saves the XML path to the AliasProperty node in the $XPath variable.

The Select-Xml cmdlet gets the AliasProperty nodes that are identified by the XPath statement from the Types.ps1xml file. The command uses a pipeline operator (|) to send the AliasProperty nodes to the Select-Object cmdlet. The ExpandProperty parameter expands the Node object and returns its Name and ReferencedMemberName properties.

$Path = "$Pshome\Types.ps1xml"
$XPath = "/Types/Type/Members/AliasProperty"
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

The result shows the Name and ReferencedMemberName of each alias property in the Types.ps1xml file. For example, there is a Count property that is an alias of the Length property.

Example 2: Input an XML document

This example shows how to use the XML parameter to provide an XML document to the Select-Xml cmdlet.

The Get-Content cmdlet gets the content of the Types.ps1xml file and saves it in the $Types variable. The [xml] casts the variable as an XML object.

The Select-Xml cmdlet gets the MethodName nodes in the Types.ps1xml file. The command uses the Xml parameter to specify the XML content in the $Types variable and the XPath parameter to specify the path to the MethodName node.

[xml]$Types = Get-Content $Pshome\Types.ps1xml
Select-Xml -Xml $Types -XPath "//MethodName"

Example 3: Search PowerShell Help files

This example shows how to use the Select-Xml cmdlet to search the PowerShell XML-based cmdlet help files. In this example, we'll search for the cmdlet name that serves as a title for each help file and the path to the help file.

The $Namespace variable contains a hash table that represents the XML namespace that is used for the help files.

The $Path variable contains the path to the PowerShell help files. If there are no help files in this path on your computer, use the Update-Help cmdlet to download the help files. For more information about Updatable Help, see about_Updatable_Help.

The Select-Xml cmdlet searches the XML files for cmdlet names by finding Command:Name element anywhere in the files. The results are stored 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 that contains the actual XML that is retrieved.

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

$Namespace = @{
    command = "http://schemas.microsoft.com/maml/dev/command/2004/10"
    maml = "http://schemas.microsoft.com/maml/2004/10"
    dev = "http://schemas.microsoft.com/maml/dev/2004/10"
}

$Path = "$Pshome\en-us\*dll-Help.xml"
$Xml = Select-Xml -Path $Path -Namespace $Namespace -XPath "//command:name"
$Xml | Format-Table @{Label="Name"; Expression= {($_.node.innerxml).trim()}}, Path -AutoSize

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
...

Example 4: Different ways to input XML

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

The first command saves a here-string that contains XML in the $Xml variable. For more information about here-strings, see about_Quoting_Rules.

Select-Xml uses the Content parameter to specify the XML in the $Xml variable.

The third command is the same as the second, except that tt uses a pipeline operator (|) to send the XML in the $Xml variable to the Select-Xml cmdlet.

$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>
"@

Select-Xml -Content $Xml -XPath "//edition" | foreach {$_.node.InnerXML}

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

$Xml | Select-Xml -XPath "//edition" | foreach {$_.node.InnerXML}

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

Example 5: Use the default xmlns namespace

This example shows how to use the Select-Xml cmdlet with XML documents that use the default xmlns namespace. The example gets the titles of Windows PowerShell ISE user-created snippet files. For information about snippets, see New-IseSnippet.

The $SnippetNamespace variable contains a hash table for the default namespace that snippet XML files use. The hash table value is the XMLNS schema URI in the snippet XML. The hash table key name, snip, is arbitrary. You can use any name that is not reserved, but you cannot use xmlns.

The Select-Xml cmdlet gets the content of the Title element of each snippet. It uses the Path parameter to specify the Snippets directory and the Namespace parameter to specify the namespace in the $SnippetNamespace variable. The value of the XPath parameter is the snip:Title. The results are piped to the ForEach-Object cmdlet, which gets the title from the value of the InnerXml property of the node.

$SnippetNamespace = @{snip = "http://schemas.microsoft.com/PowerShell/Snippets"}

Select-Xml -Path $HOME\Documents\WindowsPowerShell\Snippets -Namespace $SnippetNamespace -XPath "//snip:Title" |
    ForEach-Object {$_.Node.Innerxml}

Parameters

-Content

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

Type:String[]
Position:Named
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-LiteralPath

Specifies the paths and file names of the XML files to search. Unlike Path, the value of the LiteralPath parameter is used exactly as it is typed. No characters are interpreted as wildcards. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks tell PowerShell not to interpret any characters as escape sequences.

Type:String[]
Aliases:PSPath
Position:Named
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-Namespace

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

When the XML uses the default namespace, which begins with xmlns, use an arbitrary key for the namespace name. You cannot use xmlns. In the XPath statement, prefix each node name with the namespace name and a colon, such as //namespaceName:Node.

Type:Hashtable
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Path

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

Type:String[]
Position:1
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:True

-Xml

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.

Type:XmlNode[]
Aliases:Node
Position:1
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-XPath

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

Type:String
Position:0
Default value:None
Required:True
Accept pipeline input:False
Accept wildcard characters:False

Inputs

System.String or System.Xml.XmlNode

You can pipe a path or XML node to this cmdlet.

Outputs

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 XPath Reference and the Selection Filters section of Event Selection.