Category Archives: Xml

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

ConvertTo-Xml

NAME
    ConvertTo-Xml

SYNOPSIS
    Creates an XML-based representation of an object.

SYNTAX
    ConvertTo-Xml [-InputObject] <psobject> [-As <string>] [-Depth <int>] [-NoTypeInformation] [<CommonParameters>]

DESCRIPTION
    The ConvertTo-Xml cmdlet creates an XML-based representation of one or more Microsoft .NET Framework objects. To use this cmdlet, pipe one or more objects to the cmdlet, or use the InputObject parameter to specify the object.

    When you pipe multiple objects to ConvertTo-Xml or use the InputObject parameter to submit multiple objects, ConvertTo-Xml returns a single XML document that includes representations of all of the objects.

    This cmdlet is similar to Export-Clixml except that Export-Clixml stores the resulting XML in a file. ConvertTo-Xml returns the XML, so you can continue to process it in Windows PowerShell.

PARAMETERS
    -As <string>
        Determines the output format. Valid values are:

        — String: Returns a single string.
        — Stream: Returns an array of strings.
        — Document: Returns an XmlDocument object.

        Stream is the default.

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

    -Depth <int>
        Specifies how many levels of contained objects are included in the XML representation. The default value is 1.

        For example, if the object’s properties also contain objects, to save an XML representation of the properties of the contained objects, you must specify a depth of 2.

        The default value can be overridden for the object type in the Types.ps1xml files. For more information, see about_types.ps1xml.

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

    -InputObject <psobject>
        Specifies the object to be converted. Enter a Variable that contains the objects, or type a command or expression that gets the objects. You can also pipe objects to ConvertTo-Xml.

        Required?                    true
        Position?                    1
        Default value                None
        Accept pipeline input?     true (ByValue)
        Accept wildcard characters? true

    -NoTypeInformation [<SwitchParameter>]
        Omits the Type attribute from the object nodes.

        Required?                    false
        Position?                    named
        Default value                False
        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 ConvertTo-Xml.

OUTPUTS
    System.String or System.Xml.XmlDocument
        The value of the As parameter determines the type of object that ConvertTo-Xml returns.

NOTES

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

    C:\PS>Get-Date | ConvertTo-Xml

    Description
    ———–
    This command converts the current date (a DateTime object) to XML.

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

    C:\PS>ConvertTo-Xml -As Document -InputObject (Get-Process) -Depth 3

    Description
    ———–
    This command converts the process objects that represent all of the processes on the computer into an XML document. The objects are expanded to a depth of three levels.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=135204
    Export-Clixml
    Import-Clixml
    ConvertTo-Html
    ConvertTo-Csv