Scripting. Powershell, VMware, Windows, Active Directory & Exchange. All that kind of stuff…..
RSS icon Email icon Home icon
  • PowerShell 2.0: One Cmdlet at a Time #86 ConvertTo-XML

    Posted on April 7th, 2010 Jonathan Medd No comments

    Continuing the series looking at new cmdlets available in PowerShell 2.0. This time we look at the ConvertTo-XML cmdlet.

    What can I do with it?

    Convert a .NET object into an XML-based representation of it.

    Example:

    Retrieve a list of services beginning with the letter b and convert the object into an XML-based respresentation. Use the available Save method of the XML object to save the data into an XML file.

    $xml = Get-Service | Where-Object{$_.Name -like 'b*'} | ConvertTo-Xml
    $xml.Save("C:\temp\service.xml")

    You can see that when opened the file is a typical style XML document

    ConvertTo-XML

    How could I have done this in PowerShell 1.0?

    You could have used Export-Clixml, but that would have exported the information directly to a file which typically did not have the correct formatting when the document was viewed. It was more intended to be used in conjunction with Import-Clixml to recreate the original object from a file.

    1000 things 1% better!

    Leave a reply