PowerTip: Use PowerShell to Easily Read an XML Document

Summary: Use the type accelerator to greatly simplify reading XML documents.

Hey, Scripting Guy! Question How can I easily read an XML file?

Hey, Scripting Guy! Answer Use the [XML] type accelerator to convert results from Get-Content into an XML document, and then use dotted notation to access the nodes:

$books = Get-Content C:\fso\Books.XML


Comments (10)

  1. HicanNL says:

    Very nice. I have used this feature quite often already to import collections / packages / etc. into SCCM, based on an XML input file!

  2. jrv says:

    @all –

    Note that this will not work if the content is wrapped in a namespace.  To resolve namespaces you will need to either use the namespace manager or use the Select-XML CmdLet with its namespace support.

    I think hear Ed madly typing a blog article on this.

  3. Anonymous says:

    Hey, I’m trying to get specific fields out of an XML files of about 7GB (yes, GB). Using Get-Content and Select-Xml this ends up eating -huge- amounts of memory (70+GB). Hence, I have to find a way to stream the data and process the nodes one by one. I’ve
    come across this example, but it seems like this doesn’t allow to easily skip entire parts of the XML:


    How would you script this?

  4. Walid Toumi says:

    Hi Ed,

    thanks for sharing….here a proxy function:

    Function Get-Content {



    PS II> $file = "$PSHOMEtypes.ps1xml"

    PS II> $u = cat $file -As | Select-Xml -XP "//ScriptProperty" | Select -Expand Node

    PS II> $u

    PS II> $Xml = Get-Content $file -AsXml

    PS II> $Xml.Types.Type[1..10]

    —————————8<———————AUTHOR: Walid Toumi——-

    .ForwardHelpTargetName Get-Content

    .ForwardHelpCategory Cmdlet


    [CmdletBinding(DefaultParameterSetName='Path', SupportsTransactions=$true)]








       [Parameter(ParameterSetName='Path', Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)]



       [Parameter(ParameterSetName='LiteralPath', Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)]



















       try {

           $outBuffer = $null

           if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))


               $PSBoundParameters['OutBuffer'] = 1


           $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.ManagementGet-Content', [System.Management.Automation.CommandTypes]::Cmdlet)

           $cmd = ''

            if($AsXml) {


             $cmd += ' | ForEach-Object {$fx=@()} {$fx+=$_} {$fx -as [Xml]}'


           $ScriptCmd = [ScriptBlock]::Create(

              { & $wrappedCmd @PSBoundParameters }.ToString() + $Cmd


           $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)


       } catch {






       try {


       } catch {






       try {


       } catch {






    PS II> $xml= cat c:xmlfile.xml -asxml ; $xml

  5. Ed Wilson says:

    @Walid Toumi this is great. Thank you for sharing. Would you consider uploading this to the scripting guys script repository? It would make it eaiser to copy.

  6. Craig says:

    I'm using PowerShell 3 and tried your example without the [xml] accelerator and everything seemed to work fine.  What does the accelerator do and is really needed?

  7. Ed Wilson says:

    @Craig the [xml] type accelerator converts the text into an XMLDocument type of object. This permits navigation of the pieces of the document using a dotted notation. Without it, you only have text and not XML. See my article I wrote here: blogs.technet.com/…/use-powershell-to-simplify-access-to-xml-data.aspx

  8. Caio says:

    Excelente! Simples e direto!!! (Y)

  9. hi this is great platform to learn power shell

  10. Norma Carless says:

    Please can you tell me what I have to do to convert a text file into.
    plain English