Page 1 of 1

Splitting an XML file into smaller XML files

Posted: Wed Jan 02, 2019 1:20 pm
by releasthekraken
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

<PRINTERS>
	<PRINTER text />
	<PRINTER text />
	<PRINTER text />
</PRINTERS>
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?

Re: Splitting an XML file into smaller XML files

Posted: Wed Jan 02, 2019 1:46 pm
by jvierra
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.