PowerShell Logo Small

Select-Xml



This is the built-in help made by Microsoft for the command 'Select-Xml', in PowerShell version 5 - as retrieved from Windows version 'Microsoft Windows Server 2012 R2 Standard' PowerShell help files on 2016-06-23.

For PowerShell version 3 and up, where you have Update-Help, this command was run just before creating the web pages from the help files.

SYNOPSIS

Finds text in an XML string or document.

SYNTAX


Select-Xml [-XPath] <String> [-Xml] <XmlNode[]> [-InformationAction {SilentlyContinue | Stop | Continue | Inquire | Ignore | Suspend}] [-InformationVariable
[<System.String]>]] [-Namespace [<Hashtable>]] [<CommonParameters>]
Select-Xml [-XPath] <String> [-InformationAction {SilentlyContinue | Stop | Continue | Inquire | Ignore | Suspend}] [-InformationVariable [<System.String]>]] [-Namespace
[<Hashtable>]] -Content <String[]> [<CommonParameters>]
Select-Xml [-XPath] <String> [-Path] <String[]> [-InformationAction {SilentlyContinue | Stop | Continue | Inquire | Ignore | Suspend}] [-InformationVariable
[<System.String]>]] [-Namespace [<Hashtable>]] [<CommonParameters>]
Select-Xml [-XPath] <String> [-InformationAction {SilentlyContinue | Stop | Continue | Inquire | Ignore | Suspend}] [-InformationVariable [<System.String]>]] [-Namespace
[<Hashtable>]] -LiteralPath <String[]> [<CommonParameters>]



Search powershellhelp.space

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.



<

RELATED LINKS

Online Version: http://go.microsoft.com/fwlink/p/?linkid=294009
ConvertTo-Xml

REMARKS

<

Examples


-------------------------- EXAMPLE 1 --------------------------

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



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 third command uses the Select-Xml cmdlet to get 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.

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

PS C:\>[xml]$Types = Get-Content $pshome\Types.ps1xml
PS C:\>Select-Xml -Xml $Types -XPath "//MethodName"



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

The first command uses the Get-Content cmdlet to get the content of the Types.ps1xml file and save it in the $Types variable. The "[xml]" casts the variable as an XML object.

The second command uses the Select-Xml cmdlet to get 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.








-------------------------- EXAMPLE 3 --------------------------

The first command creates a hash table that represents the XML namespace that is used for the help files and saves it in the $Namespace variable.
PS C:\>$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"}

The second command saves the path to the help files in the $Path variable.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 ( http://go.microsoft.com/fwlink/?LinkId=235801).
PS C:\>$Path = "$pshome\en-us\*dll-Help.xml"

The third command uses the Select-Xml cmdlet to search the XML for cmdlet names by finding Command:Name element 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.
PS C:\>$Xml = Select-Xml -Path $Path -Namespace $Namespace -XPath "//command:name"

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 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.
PS C:\>$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
...



This example shows how to use the Select-Xml cmdlet to search the Windows 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.






-------------------------- EXAMPLE 4 --------------------------

The first command saves a here-string that contains XML in the $xml variable. (For more information about here-strings, see about_Quoting_Rules.)
PS C:\>$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>
"@

The second command uses the Content parameter of Select-Xml to specify the XML in the $xml variable.
PS C:\>Select-Xml -Content $xml -XPath "//edition" | foreach {$_.node.InnerXML}

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

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.
PS C:\>$xml | Select-Xml -XPath "//edition" | foreach {$_.node.InnerXML}

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



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






-------------------------- EXAMPLE 5 --------------------------

The first command creates a hash table for the default namespace the that snippet XML files use and assigns it to the $SnippetNamespace variable. 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."
PS C:\>$SnippetNamespace = @{snip="http://schemas.microsoft.com/PowerShell/Snippets"}

The second command uses the Select-Xml cmdlet to get 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" namespace key, a colon (:), and the name of
the Title element.The command uses a pipeline operator (|) to send each Node property that Select-Xml returns to the ForEach-Object cmdlet, which gets the title in the value
of the InnerXml property of the node.
PS C:\>Select-Xml –Path $home\Documents\WindowsPowerShell\Snippets –Namespace $SnippetNamespace –XPath "//snip:Title" | foreach {$_.Node.Innerxml}



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.