How to determine XML type in Powershell?

Question:

I’m writing a script that is supposed to look at the content of a file and determine if it is a (well formed) XML or not. I found a page on [ss64.com][1] that this is quite easy to do:

The thing is however that I can only test this by casting the left-side for XML files:

…which in this case would be rather pointless: if the file is XML, the casting will already prove this, else throw an exception. I therefore wonder: is there any way to determine XML files in Powershell in a True-False way?

Answer:

Try the -as operator:

Source:

How to determine XML type in Powershell? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply