Category Archives: Select

Select-Object

NAME
    Select-Object

SYNOPSIS
    Selects specified properties of an object or set of objects. It can also select unique objects from an array of objects, or it can select a specified number of objects from the beginning or end of an array of objects.

SYNTAX
    Select-Object [[-Property] <Object[]>] [-ExcludeProperty <string[]>] [-ExpandProperty <string>] [-First <int>] [-InputObject <psobject>] [-Last <int>] [-Skip <int>] [-Unique] [<CommonParameters>]

    Select-Object [-Index <Int32[]>] [-InputObject <psobject>] [-Unique] [<CommonParameters>]

DESCRIPTION
    The Select-Object cmdlet gets only the specified properties of an object or set of objects. It can also select unique objects from an array of objects, or it can select a specified number of objects from the beginning or end of an array of objects.

    If you use Select-Object to select specified properties, it copies the values of those properties from the input objects and creates new objects that have the specified properties and copied values.

    Use the Property parameter to specify the properties you want to select. Or, use the First, Last, Unique, Skip, and Index parameters to select particular objects from an array of input objects. For more specific object filtering, use the Where-Object cmdlet.

PARAMETERS
    -ExcludeProperty <string[]>
        Removes the specifies properties from the selection. Wildcards are permitted. This parameter is effective only when the command also includes the Property parameter.

        The value of the property parameter can be a calculated property, which is a hash table that specifies a name and calculates a value for the property display. Valid keys are:

         — Name or Label <string>
         — Expression <string> or <scriptblock>

        For more information, see the examples.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? true

    -ExpandProperty <string>
        Specifies a property to select, and indicates that an attempt should be made to expand that property. Wildcards are permitted in the property name.

        For example, if the specified property is an array, each value of the array is included in the output. If the property contains an object, the properties of that object are displayed in the output.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? true

    -First <int>
        Specifies the number of objects to select from the beginning of an array of input objects.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Index <Int32[]>
        Selects objects from an array based on their index values. Enter the indexes in a comma-separated list.

        Indexes in an array begin with 0, where 0 represents the first value and (n-1) represents the last value.

        Required?                    false
        Position?                    named
        Default value                None
        Accept pipeline input?     false
        Accept wildcard characters? false

    -InputObject <psobject>
        Specifies objects to send to the cmdlet through the pipeline. This parameter enables you to pipe objects to Select-Object.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     true (ByValue)
        Accept wildcard characters? false

    -Last <int>
        Specifies the number of objects to select from the end of an array of input objects.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Property <Object[]>
        Specifies the properties to select. Wildcards are permitted.

        The value of the Property parameter can be a new calculated property. To create a calculated, property, use a hash table. Valid keys are:
        — Name (or Label) <string>
        — Expression <string> or <script block>

        Required?                    false
        Position?                    1
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? true

    -Skip <int>
        Skips (does not select) the specified number of items. By default, the Skip parameter counts from the beginning of the array or list of objects, but if the command uses the Last parameter, it counts from the end of the list or array.

        Unlike the Index parameter, which starts counting from 0, the Skip parameter begins at 1.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Unique [<SwitchParameter>]
        Specifies that if a subset of the input objects has identical properties and values, only a single member of the subset will be selected.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    System.Management.Automation.PSObject
        You can pipe any object to Select-Object.

OUTPUTS
    System.Management.Automation.PSObject

NOTES

        You can also refer to Select-Object by its built-in Alias, “select”. For more information, see about_aliases.

    ————————– EXAMPLE 1 ————————–

    C:\PS>Get-Process | Select-Object ProcessName,Id,WS

    Description
    ———–
    This command displays a list of processes. Only the name, ID, and working set (WS) properties of the processes are displayed.

    ————————– EXAMPLE 2 ————————–

    C:\PS>Get-Process | Select-Object processname -ExpandProperty modules |
    Format-List

    ProcessName     : 00THotkey
    Size             : 256
    Company         : TOSHIBA Corporation
    FileVersion     : 1, 0, 0, 27
    ProductVersion    : 6, 2, 0, 0
    Description     : THotkey
    Product         : TOSHIBA THotkey
    ModuleName        : 00THotkey.exe
    FileName         : C:\WINDOWS\system32\00THotkey.exe
    BaseAddress     : 4194304

    Description
    ———–
    This command displays information about the modules used by the processes running on a computer. It uses the ExpandProperty parameter to display the details contained within the modules property.

    ————————– EXAMPLE 3 ————————–

    C:\PS>Get-Process | Sort-Object -Property WS | Select-Object -Last 5

    Handles NPM(K)    PM(K)     WS(K) VS(M) CPU(s)     Id ProcessName
    ——- ——    —–     —– —– ——     — ———–
     2866     320    33432     45764 203 222.41 1292 svchost
        577     17    23676     50516 265    50.58 4388 WINWORD
        826     11    75448     76712 188    19.77 3780 Ps
     1367     14    73152     88736 216    61.69    676 Ps
     1612     44    66080     92780 380 900.59 6132 INFOPATH

    Description
    ———–
    This command displays the five processes that are using the most memory. The Sort-Object cmdlet is used to sort the processes according to memory (working set) usage, and the Select-Object cmdlet is used to select only the last five members of the resulting array of objects.

    ————————– EXAMPLE 4 ————————–

    C:\PS>Get-Process | Select-Object -Property ProcessName,@{Name=”Start Day”; Expression = {$_.StartTime.DayOfWeek}}

    ProcessName StartDay
    —-         ——–
    alg         Wednesday
    ati2evxx     Wednesday
    ati2evxx     Thursday
    …

    Description
    ———–
    This command displays the name and start day of the processes running on a computer.

    The values of the Property parameter are ProcessName and a calculated property named “Start Day.” The “Start Day” property is added by using a hash table with Name and Expression keys.

    ————————– EXAMPLE 5 ————————–

    C:\PS>”a”,”b”,”c”,”a”,”a”,”a” | Select-Object -Unique

    a
    b
    c

    Description
    ———–
    This command displays unique characters from an array of characters.

    ————————– EXAMPLE 6 ————————–

    C:\PS>$a = Get-Eventlog -log “Windows PowerShell”

    C:\PS> $a | Select-Object -index 0, ($a.count – 1)

    Description
    ———–
    These commands get the first (newest) and last (oldest) events in the Windows Powershell event log.

    The first command uses the Get-EventLog cmdlet to get all events in the Windows Powershell log. It saves the events in the $a Variable.

    The second command uses a pipeline operator (|) to send the events in $a to the Select-Object cmdlet. The Select-Object command uses the Index parameter to select items by their index number. The index for the first event is 0. The index for the last event is the number of items in $a minus 1.

    ————————– EXAMPLE 7 ————————–

    C:\PS>New-PSSession -computername (Get-Content servers.txt | Select-Object -Skip 1)

    Description
    ———–
    This command creates a new PSSession on each of the computers listed in the Servers.txt files, except for the first one.

    This command uses the Select-Object cmdlet to select all but the first computer in a list of computer names. The resulting list of computers is set as the value of the ComputerName parameter of the New-PSSession cmdlet.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113387
    Where-Object
    Group-Object
    Sort-Object

Select-String

NAME
    Select-String

SYNOPSIS
    Finds text in strings and files.

SYNTAX
    Select-String [-Path] <string[]> [-Pattern] <string[]> [-AllMatches] [-CaseSensitive] [-Context <Int32[]>] [-Encoding <string>] [-Exclude <string[]>] [-Include <string[]>] [-List] [-NotMatch] [-Quiet] [-SimpleMatch] [<CommonParameters>]

    Select-String -InputObject <psobject> [-Pattern] <string[]> [-AllMatches] [-CaseSensitive] [-Context <Int32[]>] [-Encoding <string>] [-Exclude <string[]>] [-Include <string[]>] [-List] [-NotMatch] [-Quiet] [-SimpleMatch] [<CommonParameters>]

DESCRIPTION
    The Select-String cmdlet searches for text and text patterns in input strings and files. You can use it like Grep in UNIX and Findstr in Windows.

    Select-String is based on lines of text. By default, Select-String finds the first match in each line and, for each match, it displays the file name, line number, and all text in the line containing the match.

    However, you can direct it to detect multiple matches per line, display text before and after the match, or display only a Boolean value (true or false) that indicates whether a match is found.

    Select-String uses regular expression matching, but it can also perform a simple match that searches the input for the text that you specify.

    Select-String can display all of the text matches or stop after the first match in each input file. It can also display all text that does not match the specified pattern.

    You can also specify that Select-String should expect a particular character encoding, such as when you are searching files of Unicode text.

PARAMETERS
    -AllMatches [<SwitchParameter>]
        Searches for more than one match in each line of text. Without this parameter, Select-String finds only the first match in each line of text.

        When Select-String finds more than one match in a line of text, it still emits only one MatchInfo object for the line, but the Matches property of the object contains all of the matches.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -CaseSensitive [<SwitchParameter>]
        Makes matches case-sensitive. By default, matches are not case-sensitive.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Context <Int32[]>
        Captures the specified number of lines before and after the line with the match. This allows you to view the match in context.

        If you enter one number as the value of this parameter, that number determines the number of lines captured before and after the match. If you enter two numbers as the value, the first number determines the number of lines before the match and the second number determines the number of lines after the match.

        In the default display, lines with a match are indicated by a right angle bracket (ASCII 62) in the first column of the display. Unmarked lines are the context.

        This parameter does not change the number of objects generated by Select-String. Select-String generates one MatchInfo (Microsoft.PowerShell.Commands.MatchInfo) object for each match. The context is stored as an array of strings in the Context property of the object.

        When you pipe the output of a Select-String command to another Select-String command, the receiving command searches only the text in the matched line (the value of the Line property of the MatchInfo object), not the text in the context lines. As a result, the Context parameter is not valid on the receiving Select-String command.

        When the context includes a match, the MatchInfo object for each match includes all of the context lines, but the overlapping lines appear only once in the display.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Encoding <string>
        Specifies the character encoding that Select-String should assume when searching the file. The default is UTF8.

        Valid values are “UTF7”, “UTF8”, “UTF32”, “ASCII”, “Unicode”, “BigEndianUnicode”, “Default”, and “OEM”. “Default” is the encoding of the system’s current ANSI code page. “OEM” is the current original equipment manufacturer code page identifier for the operating system.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Exclude <string[]>
        Exclude the specified items. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as “*.txt”. Wildcards are permitted.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Include <string[]>
        Include only the specified items. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as “*.txt”. Wildcards are permitted.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -InputObject <psobject>
        Specifies the text to be searched. Enter a Variable that contains the text, or type a command or expression that gets the text.

        Required?                    true
        Position?                    named
        Default value
        Accept pipeline input?     true (ByValue)
        Accept wildcard characters? false

    -List [<SwitchParameter>]
        Returns only the first match in each input file. By default, Select-String returns a MatchInfo object for each match it finds.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -NotMatch [<SwitchParameter>]
        Finds text that does not match the specified pattern.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Path <string[]>
        Specifies the path to the files to be searched. Wildcards are permitted. The default location is the local directory.

        Specify files in the directory, such as “log1.txt”, “*.doc”, or “*.*”. If you specify only a directory, the command fails.

        Required?                    true
        Position?                    2
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -Pattern <string[]>
        Specifies the text to find. Type a string or regular expression. If you type a string, use the SimpleMatch parameter.

        To learn about regular expressions, see about_regular_expressions.

        Required?                    true
        Position?                    1
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Quiet [<SwitchParameter>]
        Returns a Boolean value (true or false), instead of a MatchInfo object. The value is “true” if the pattern is found; otherwise, the value is “false”.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -SimpleMatch [<SwitchParameter>]
        Uses a simple match rather than a regular expression match. In a simple match, Select-String searches the input for the text in the Pattern parameter. It does not interpret the value of the Pattern parameter as a regular expression statement.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    System.Management.Automation.PSObject
        You can pipe any object that has a ToString method to Select-String.

OUTPUTS
    Microsoft.PowerShell.Commands.MatchInfo or System.Boolean
        By default, the output is a set of MatchInfo objects, one for each match found. If you use the Quiet parameter, the output is a Boolean value indicating whether the pattern was found.

NOTES

        Select-String is like the Grep command in UNIX and the FindStr command in Windows.

        To use Select-String, type the text that you want to find as the value of the Pattern parameter.

        To specify the text to be searched, do the following:

        — Type the text in a quoted string, and then pipe it to Select-String.
        — Store a text string in a Variable, and then specify the Variable as the value of the InputObject parameter.
        — If the text is stored in files, use the Path parameter to specify the path to the files.

        By default, Select-String interprets the value of the Pattern parameter as a regular expression. (For more information, see about_regular_expressions.) However, you can use the SimpleMatch parameter to override the regular expression matching. The SimpleMatch parameter finds instances of the value of the Pattern parameter in the input.

        The default output of Select-String is a MatchInfo object, which includes detailed information about the matches. The information in the object is useful when you are searching for text in files, because MatchInfo objects have properties such as Filename and Line. When the input is not from the file, the value of these parameters is “InputStream”.

        If you do not need the information in the MatchInfo object, use the Quiet parameter, which returns a Boolean value (true or false) to indicate whether it found a match, instead of a MatchInfo object.

        When matching phrases, Select-String uses the current that is set for the system. To find the current culture, use the Get-Culture cmdlet.

        To find the properties of a MatchInfo object, type the following:

        Select-String -path test.txt -Pattern “test” | Get-Member | Format-List -property *

    ————————– EXAMPLE 1 ————————–

    C:\PS>”Hello”,”HELLO” | Select-String -Pattern “HELLO” -CaseSensitive

    Description
    ———–
    This command performs a case-sensitive match of the text that was piped to the Select-String command.

    As a result, Select-String finds only “HELLO”, because “Hello” does not match.

    Because each of the quoted strings is treated as a line, without the CaseSensitive parameter, Select-String would recognize both of the strings as matches.

    ————————– EXAMPLE 2 ————————–

    C:\PS>Select-String -path *.xml -Pattern “the the”

    Description
    ———–
    This command searches through all files with the .xml file name extension in the current directory and displays the lines in those files that include the string “the the”.

    ————————– EXAMPLE 3 ————————–

    C:\PS>Select-String -path $pshome\en-US\*.txt -Pattern “@”

    Description
    ———–
    This command searches the Windows PowerShell conceptual Help files (about_*.txt) for information about the use of the at sign (@).

    To indicate the path, this command uses the value of the $pshome automatic Variable, which stores the path to the Windows PowerShell installation directory. In this example, the command searches the en-US subdirectory, which contains the English (US) language Help files for Windows PowerShell.

    ————————– EXAMPLE 4 ————————–

    C:\PS>function search-help
    {
        $pshelp = “$pshome\es\about_*.txt”, “$pshome\en-US\*dll-help.xml”
        Select-String -path $pshelp -Pattern $args[0]
    }

    Description
    ———–
    This simple Function uses the Select-String cmdlet to search the Windows PowerShell Help files for a particular string. In this example, the Function searches the “en-US” subdirectory for English-United States language files.

    To use the Function to find a string, such as “psdrive”, type “search-help psdrive”.

    To use this Function in any Windows PowerShell console, change the path to point to the Windows PowerShell Help files on your system, and then paste the Function in your Windows PowerShell profile.

    ————————– EXAMPLE 5 ————————–

    C:\PS>$events = Get-Eventlog -logname application -newest 100

    C:\PS> $events | Select-String -InputObject {$_.message} -Pattern “failed”

    Description
    ———–
    This example searches for the string “failed” in the 100 newest events in the Application log in Event Viewer.

    The first command uses the Get-EventLog cmdlet to get the 100 most recent events from the Application event log. Then it stores the events in the $events Variable.

    The second command uses a pipeline operator (|) to send the objects in the $events Variable to Select-String. It uses the InputObject parameter to represent the input from the $events Variable. The value of the InputObject parameter is the Message property of each object as it travels through the pipeline. The current object is represented by the $_ symbol.

    As each event arrives in the pipeline, Select-String searches the value of its Message property for the “failed” string, and then displays any lines that include a match.

    ————————– EXAMPLE 6 ————————–

    C:\PS>Get-ChildItem c:\windows\system32\* -Include *.txt -recurse |
    Select-String -Pattern “Microsoft” -CaseSensitive

    Description
    ———–
    This command examines all files in the subdirectories of C:\Windows\System32 with the .txt file name extension and searches for the string “Microsoft”. The CaseSensitive parameter indicates that the “M” in “Microsoft” must be capitalized and that the rest of the characters must be lowercase for Select-String to find a match.

    ————————– EXAMPLE 7 ————————–

    C:\PS>Select-String -path process.txt -Pattern idle, svchost -NotMatch

    Description
    ———–
    This command finds lines of text in the Process.txt file that do not include the words “idle” or “svchost”.

    ————————– EXAMPLE 8 ————————–

    C:\PS>$f = Select-String -path audit.log -Pattern “logon failed” -Context 2, 3

    C:\PS> $f.count

    C:\PS> ($f)[0].context | Format-List

    Description
    ———–
    The first command searches the Audit.Log file for the phrase “logon failed.” It uses the Context parameter to capture 2 lines before the match and 3 lines after the match.

    The second command uses the Count property of object arrays to display the number of matches found, in this case, 2.

    The third command displays the lines stored in the Context property of the first MatchInfo object. It uses array notation to indicate the first match (match 0 in a zero-based array), and it uses the Format-List cmdlet to display the value of the Context property as a list.

    The output consists of two MatchInfo objects, one for each match detected. The context lines are stored in the Context property of the MatchInfo object.

    ————————– EXAMPLE 9 ————————–

    C:\PS>$a = Get-ChildItem $pshome\en-us\about*.help.txt | Select-String -Pattern transcript

    C:\PS> $b = Get-ChildItem $pshome\en-us\about*.help.txt | Select-String -Pattern transcript -AllMatches

    C:\PS> $a
    C:\Windows\system32\WindowsPowerShell\v1.0\en-us\about_Pssnapins.help.txt:39:     Start-Transcript and Stop-Transcript.

    C:\PS> $b
    C:\Windows\system32\WindowsPowerShell\v1.0\en-us\about_Pssnapins.help.txt:39:     Start-Transcript and Stop-Transcript.

    C:\PS>> $a.matches
    Groups : {Transcript}
    Success : True
    Captures : {Transcript}
    Index    : 13
    Length : 10
    Value    : Transcript

    C:\PS> $b.matches
    Groups : {Transcript}
    Success : True
    Captures : {Transcript}
    Index    : 13
    Length : 10
    Value    : Transcript

    Groups : {Transcript}
    Success : True
    Captures : {Transcript}
    Index    : 33
    Length : 10
    Value    : Transcript

    Description
    ———–
    This example demonstrates the effect of the AllMatches parameter of Select-String. AllMatches finds all pattern matches in a line, instead of just finding the first match in each line.

    The first command in the example searches the Windows PowerShell conceptual Help files (“about” Help) for instances of the word “transcript”. The second command is identical, except that it uses the AllMatches parameter.

    The output of the first command is saved in the $a Variable. The output of the second command is saved in the $b Variable.

    When you display the value of the Variables, the default display is identical, as shown in the example output.

    However, the fifth and sixth commands display the value of the Matches property of each object. The Matches property of the first command contains just one match (that is, one System.Text.RegularExpressions.Match object), whereas the Matches property of the second command contains objects for both of the matches in the line.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113388
    about_Comparison_Operators
    about_regular_expressions

Select-Xml

NAME
    Select-Xml

SYNOPSIS
    Finds text in an XML string or document.

SYNTAX
    Select-Xml -Content <string[]> [-XPath] <string> [-Namespace <hashtable>] [<CommonParameters>]

    Select-Xml [-Path] <string[]> [-XPath] <string> [-Namespace <hashtable>] [<CommonParameters>]

    Select-Xml [-Xml] <XmlNode[]> [-XPath] <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. A Path or XML parameter is required in every command.

        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: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    System.String or System.Xml.XmlNode
        You can pipe a path or XML node to Select-Xml.

OUTPUTS
    System.Xml.XmlElement or System.Xml.XmlText

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 http://go.microsoft.com/fwlink/?LinkId=143608. And, see “XPath Reference” in the MSDN library at http://go.microsoft.com/fwlink/?LinkId=143609.

    ————————– EXAMPLE 1 ————————–

    C:\PS>$path = “$env:windir\System32\WindowsPowerShell\v1.0\Types.ps1xml”

    C:\PS> Select-Xml -path $path -XPath “/Types/Type/Members/AliasProperty”

    Description
    ———–
    This example searches the Types.ps1xml file for child items of the AliasProperty node.

    ————————– 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 Search-Path.

    ————————– EXAMPLE 4 ————————–

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

    C:\PS> $path = “$env:windir\System32\WindowsPowerShell\V1.0\en-us\*dll-Help.xml”

    C:\PS> Select-Xml -path $path -Namespace $namespace -XPath “//command:name”

    Text                     Node     Path
    —-                     —-     —-
    Add-Computer             name     C:\Windows\System32\WindowsPowerShell\V…
    Add-Content             name     C:\Windows\System32\WindowsPowerShell\V…
    Checkpoint-Computer     name     C:\Windows\System32\WindowsPowerShell\V…
    Clear-Content             name     C:\Windows\System32\WindowsPowerShell\V…
    Clear-EventLog            name     C:\Windows\System32\WindowsPowerShell\V…
    …

    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.

    ————————– EXAMPLE 5 ————————–

    C:\PS>Select-Xml -content $xml -XPath “//edition”

    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”

    Text            Node         Path
    —-            —-         —-
    En.Book1.com    edition     InputStream
    Ge.Book1.Com    edition     InputStream
    Fr.Book1.com    edition     InputStream
    Pl.Book1.com    edition     InputStream

    C:\PS> $xml | Select-Xml -XPath “//edition”

    Text            Node         Path
    —-            —-         —-
    En.Book1.com    edition     InputStream
    Ge.Book1.Com    edition     InputStream
    Fr.Book1.com    edition     InputStream
    Pl.Book1.com    edition     InputStream

    Description
    ———–
    This example uses the Content parameter of Select-Xml to search XML content in a here-string.

    The first command saves the here-string in the $xml Variable.

    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.

    For more information about here-strings, type about_Quoting_Rules.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=135255
    ConvertTo-Xml