Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

PowerShell save export data to XML file

I wanted to create XML file using PowerShell and PowerShell offers different ways to create XML file in different formats, with PowerShell you can create and generate different types of XML format. One of the simple way I shown in this article PowerShell Create XML document with XmlWriter .net object

Export-Clixml

In the below example the Export-Clixml cmdlet generates a Common Language Infrastructure (CLI) XML-based representation of an object or objects and saves/stores it in a file. In the produced file the XML schema used is PowerShell. A valued use of Export-Clixml on Windows computers is to export credentials and secure/protect strings securely as XML.

#Export-CliXML
Get-Service | Select-Object Name, DisplayName, Status, StartType -First 2 | Export-Clixml -NoTypeInformation -Path C:\Temp\XML\file.xml
notepad C:\Temp\XML\file.xml

Microsoft Powershell Get-Service Select-Object pipeline name displayname status starttype first export-clixml notypeinformation convert to xml.png

Microsoft Powershell file.xml schemas xml powershell export-clixml import-clixml system.object serialization convertto-xml save file.png

ConvertTo-Xml

This is the another way to create XML with cmdlet ConvertTo-Xml. It generates XML-based illustration of one or more .NET objects. To use this cmdlet, pipe one or more objects to the cmdlet, or use the InputObject parameter to specify the object. When you pipe manifold objects to ConvertTo-Xml or use the InputObject parameter to submit multiple objects, ConvertTo-Xml returns a single, in-memory XML document that comprises representations of all of the objects.

Inside ConvertTo-Xml there is method called save('filepath'), to save file to provided path.

Microsoft Powershell filepath xmldata Get-Service Select-object displayname status startup convertto-XML -notypeinformation save() property object encoding utf-8 xml save file convert export file.png

#ConvertTo-XML
$filePath = 'C:\Temp\XML\file.xml'
$xmlData = Get-Service | Select-Object Name, DisplayName, Status, StartType -First 2 | ConvertTo-XML -NoTypeInformation
$xmlData.Save($filePath)
Get-Content $filePath

Generate XML file using foreach loop

Below XML data format is very simple and alternate to generate XML file. I have generated this file using below sample foreach loop. You can generate same format in the file using PowerShell Create XML document with XmlWriter .net object.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#Manually create XML using foreach loop
$data = Get-Service | Select-Object Name, DisplayName, Status, StartType -First 2
$properties = $data | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty  Name

$xmlData = @()
$xmlData += '<?xml version="1.0" encoding="UTF-8"?> '
$xmlData += '<services>'
foreach ($obj in $data) 
{
    $xmlData += '   <service>'
    foreach ($property in $properties) 
    {
        $xmlData += "       <$property>$($obj.$property)</$property>"
    }
    $xmlData += '   </service>'
}
$xmlData += '</services>'
$xmlData | Out-File -FilePath C:\Temp\XML\file.xml

Microsoft Powershell XML services foreach-object get-service properties get-member arraylist loop automation xml format simple.png

Download convert to xml using powershell here or this script is also available on github.com.

Useful Articles
PowerShell slice array into groups of smaller arrays
Powershell web scrapping extract table from html
Powershell adding leading zeros to string or int
PowerShell convert string to base64 value
PowerShell Encode or Decode an WebURL
Create an interactive HTML report with PowerShell data

Go Back

Comment

Blog Search

Page Views

11274991

Follow me on Blogarama