Prof. Powershell

PowerShell to XML

That nifty trick I showed you last time that exports data to CSV? This time, get more with an export to XML.

Last time I showed you how to export data from PowerShell to a CSV file. The format is easy to use but that simplicity sacrifices object detail. For example, an object with nested objects doesn't export very well to a CSV file. Some information is "lost" in the translation.

A richer format is XML. Any place that you could use Export-CSV you could use its XML cousin, Export-cliXML:

PS C:\> get-wmiobject win32_process | export-clixml wmiprocs.xml

Unlike an exported CSV file, which you could use in other applications, an XML file created with Export-cliXML can only really be used within PowerShell. You can use this cmdlet to serialize objects, like the Win32_Process objects in my example. I can "re-create" these objects at any time in any PowerShell session using Import-cliXML:

PS C:\> import-clixml wmiprocs.xml

Like Import-CSV, I can use these imported objects like any other object:

PS C:\> import-clixml wmiprocs.xml | where {$_.workingsetsize -gt 10MB} | Sort WorkingSetSize | Select Name,*Set*

As I mentioned, the XML format allows you to capture objects with greater detail. Consider this example:

PS C:\> get-service | export-csv services.csv

If you try the export using Export-CSV and then re-import, you'll see that some information is lost, such as here:

PS C:\> import-csv services.csv | select name,dependendServices

Name                 DependentServices
AcrSch2Svc           System.ServiceProcess.ServiceControl...
AeLookupSvc          System.ServiceProcess.ServiceControl...
AgereModemAudio      System.ServiceProcess.ServiceControl...
ALG                  System.ServiceProcess.ServiceControl...
Appinfo              System.ServiceProcess.ServiceControl...
AppMgmt              System.ServiceProcess.ServiceControl...
AudioEndpointBuilder System.ServiceProcess.ServiceControl...
Audiosrv             System.ServiceProcess.ServiceControl...
avg8wd               System.ServiceProcess.ServiceControl...
BFE                  System.ServiceProcess.ServiceControl...
...

Now see how the XML format captures more information:

PS C:\> get-service | export-clixml services.xml
PS C:\> import-clixml services.xml | select ServiceName,DependentServices

ServiceName          DependentServices
-----------          -----------------
AcrSch2Svc           {}
AeLookupSvc          {}
AgereModemAudio      {}
ALG                  {}
Appinfo              {}
AppMgmt              {}
AudioEndpointBuilder {Windows Audio}
Audiosrv             {}
avg8wd               {}
BFE                  {Internet Connection Sharing (ICS),
                      Routing and Remote A.
BITS                 {}
...

The reconstituted service objects are of a different type, which you can confirm by piping them to Get-Member, which is why I need to use the ServiceName property instead of name. But you can see that the DependentService object property is now properly expanded.

If you prefer to limit how much detail the cmdlet stores, you can control it with the -depth parameter. Personally, if I'm exporting data I don't want to lose any of it.

Exporting objects to XML is a great technique for archiving, troubleshooting or offline analysis, because so much of the original object is retained. For more information on these cmdlets be sure to look at their full help and examples.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular