Splitting an XML file into smaller XML files

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
Posts: 1
Joined: Mon Apr 23, 2018 5:24 pm

Splitting an XML file into smaller XML files

Post by releasthekraken » Wed Jan 02, 2019 2:20 pm

I am trying to split one XML file into multiple smaller XML files using PowerShell. There are a very large number of "PRINTER" nodes. I would like to create a new XML for every 150 "PRINTER" nodes. The Structure for the file is very simple like below:

Code: Select all

	<PRINTER text />
	<PRINTER text />
	<PRINTER text />
Iv'e been trying to do this with substrings and creating new arrays but it's getting very messy and I'm still having trouble with it. Can anybody provide some insight into an easier way to do this?

User avatar
Posts: 13486
Joined: Tue May 22, 2007 9:57 am

Re: Splitting an XML file into smaller XML files

Post by jvierra » Wed Jan 02, 2019 2:46 pm

Start with loading the file as XML.

[xml]$xml = Get-Content printers.xml

Now you can just group the printers and copy to a new XML file.

$group1 = $xml.Printers.Printer[0..149]

Add the Group to a new file after creating it and importing the nodes to copy.