ISE Addons, PowerShell, Scripting
comment 1

PowerShell ISE Addon: Validate and Format XML

I have been playing with XML documents a lot these days and at times I find myself dealing with a few pages long XML data. The most frustrating part is the un-formatted and invalid XML. Many times, I find myself dealing with XML errors than any script errors. Fortunately, PowerShell ISE in version 3.0 provides, XML syntax checks.

But, here is the catch:

These syntax checks work only with saved files and only if the file name ends with .xml. Also, it is quite possible to overlook these errors in an XML document that is a few hundred lines long. Also, there is no way to easily and automatically indent a XML document.

So, here is another addon to make life easy when using XML documents in PowerShell ISE. This addon actually comes with a couple of functions.

Update: Updated the post to include support for selected text. Now, you can select an XML snippet in your script and then validate it or format it.

Here is quick video that shows how this addon works:

Validating XML content

I used the C# sample available on this MSDN pageto build the PowerShell script for validating XML document. This is not complete yet. It can only verify if all required tags are in place or not and does not compare the XML data against any schema definition you have.

Formatting the XML content

This function returns the formatted XML output. Thanks to the PowerShell team for this pointer. I did not really have to write anything myself for formatting XML. Now, I added these two to my ISE profile and created addon menus that use these functions.

This is it. Now, whenever I need to validate XML content, I can simply press Ctrl+Alt+X and Alt+X, when I want to format the XML document.

Filed under: ISE Addons, PowerShell, Scripting


Ravikanth is a principal engineer and the lead architect for Microsoft and VMware virtualized and hybrid cloud solutions within the Infrastructure Solutions Group at Dell EMC. He is a multi-year recipient of Microsoft Most Valuable Professional (MVP) award in Windows PowerShell (CDM) and Microsoft Azure. Ravikanth is the author of Windows PowerShell Desired State Configuration Revealed (Apress) and leads Bangalore PowerShell and Bangalore IT Pro user groups. He can be seen speaking regularly at local user group events and conferences in India and abroad.