Splitting an XML file into smaller XML files

Ask your 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.
This topic is 5 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
releasthekraken
Posts: 1
Last visit: Thu Jan 03, 2019 2:47 pm

Splitting an XML file into smaller XML files

Post 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?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Splitting an XML file into smaller XML files

Post 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.
This topic is 5 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked