Create XML Files with Content Using PowerShell

I often work with XML files for various tasks as a PowerShell enthusiast. Whether it’s storing configuration settings, generating reports, or exchanging data between systems, XML is the correct format that offers structure and flexibility. In this tutorial, I will explain different methods for creating XML files with content using PowerShell.

Method 1: Using the XmlTextWriter Class

One of the best ways to create XML documents in PowerShell is by utilizing the XmlTextWriter class. This class provides a convenient way to write XML elements and attributes programmatically. Here’s an example:

$filePath = "/Users/bijay/Downloads/employees.xml"
$xmlWriter = New-Object System.Xml.XmlTextWriter($filePath, $null)

$xmlWriter.WriteStartDocument()
$xmlWriter.WriteStartElement("Users")

$xmlWriter.WriteStartElement("User")
$xmlWriter.WriteAttributeString("Name", "John Doe")
$xmlWriter.WriteAttributeString("Email", "john.doe@example.com")
$xmlWriter.WriteEndElement()

$xmlWriter.WriteStartElement("User")
$xmlWriter.WriteAttributeString("Name", "Jane Smith")
$xmlWriter.WriteAttributeString("Email", "jane.smith@example.com")
$xmlWriter.WriteEndElement()

$xmlWriter.WriteEndElement()
$xmlWriter.WriteEndDocument()
$xmlWriter.Flush()
$xmlWriter.Close()

In this example, we create an instance of the XmlTextWriter class, specifying the file path where the XML file will be saved. We then use various methods like WriteStartElement, WriteAttributeString, and WriteEndElement to define the structure and content of the XML document. Finally, we flush the writer and close it to ensure the XML file is properly written to disk.

I executed the above PowerShell script in my macOS laptop and you can see the exact output in the screenshot below:

How to Create XML Files with Content Using PowerShell

Check out Remove Blank Lines from CSV Files Using PowerShell

Method 2: Using the XmlDocument Class

Another approach to creating XML files in PowerShell is by using the XmlDocument class. This class allows you to build an in-memory representation of the XML document and then save it to a file. Here’s an example:

$xmlDoc = New-Object System.Xml.XmlDocument

$rootElement = $xmlDoc.CreateElement("Employees")
$xmlDoc.AppendChild($rootElement)

$employee1 = $xmlDoc.CreateElement("Employee")
$employee1.SetAttribute("Name", "Michael Johnson")
$employee1.SetAttribute("Department", "Sales")
$rootElement.AppendChild($employee1)

$employee2 = $xmlDoc.CreateElement("Employee")
$employee2.SetAttribute("Name", "Emily Davis")
$employee2.SetAttribute("Department", "Marketing")
$rootElement.AppendChild($employee2)

$xmlDoc.Save("/Users/bijay/Downloads/employees.xml")

In this example, we create an instance of the XmlDocument class and use its methods to construct the XML structure. We create elements using the CreateElement method, set attributes using SetAttribute, and append child elements using AppendChild. Once the XML document is built, we save it to a file using the Save method.

You can see the exact output in the screenshot below; it created the XML file with data in the required file path.

Create XML Files with Content Using PowerShell

Check out Create JSON Files with Content Using PowerShell

Creating XML Configuration Files

XML files are commonly used for storing configuration settings. PowerShell makes it easy to create and manage XML configuration files. Here’s an example of how you can create an XML configuration file:

$configData = @{
    AppName = "My Application"
    Version = "1.0"
    Database = @{
        Server = "localhost"
        Name = "MyDatabase"
        User = "admin"
        Password = "password123"
    }
}

$configXml = New-Object System.Xml.XmlDocument
$configXml.AppendChild($configXml.CreateXmlDeclaration("1.0", "UTF-8", $null))

$rootElement = $configXml.CreateElement("Configuration")
$configXml.AppendChild($rootElement)

foreach ($key in $configData.Keys) {
    $element = $configXml.CreateElement($key)
    if ($configData[$key] -is [hashtable]) {
        foreach ($subKey in $configData[$key].Keys) {
            $subElement = $configXml.CreateElement($subKey)
            $subElement.InnerText = $configData[$key][$subKey]
            $element.AppendChild($subElement)
        }
    } else {
        $element.InnerText = $configData[$key]
    }
    $rootElement.AppendChild($element)
}

$configXml.Save("C:\Users\SarahWilson\Documents\config.xml")

In this example, we define a hashtable $configData that holds the configuration settings. We then create an XmlDocument instance and populate it with the configuration data.

We handle nested settings by recursively creating elements and appending them to the parent element. Finally, we save the XML configuration file to disk.

Conclusion

In this tutorial, I explained multiple ways to create XML files with content in PowerShell. You also learned how to use the XmlTextWriter and XmlDocument class to create XML files with content in PowerShell.

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.