ZetCode

PowerShell Export-Clixml

last modified February 15, 2025

This article covers the Export-Clixml cmdlet in PowerShell. It serializes objects to XML files for storage or transfer. The cmdlet preserves object structure and properties. It's useful for saving complex data between sessions.

CLIXML basics

CLIXML is PowerShell's custom XML-based serialization format. It maintains object structure and type information. Export-Clixml creates files that can be imported with Import-Clixml. The format is optimized for PowerShell objects. It's not intended for cross-platform use.

Basic Export-Clixml usage

The simplest usage exports a single object to an XML file. Specify the input object and output file path. The cmdlet automatically creates the XML representation. The file can be later imported to recreate the object.

export1.ps1
Get-Process -Name "notepad" | Export-Clixml -Path "notepad_process.xml"

This exports Notepad process information to an XML file. The output contains all process properties. The file can be imported to recreate the Process object.

Exporting multiple objects

Export-Clixml can handle collections of objects. The entire collection is serialized to a single file. When imported, the original collection structure is preserved. This works with arrays and other collections.

export2.ps1
Get-Process | Where-Object { $_.CPU -gt 10 } | Export-Clixml -Path "busy_processes.xml"

This exports all processes using more than 10 CPU seconds. The resulting file contains an array of Process objects. Each maintains its properties and methods.

Exporting custom objects

You can export any PowerShell object, including custom ones. This example creates and exports a custom object. The XML preserves all property values. The object type information is also stored.

export3.ps1
$user = [PSCustomObject]@{
    Name = "John Doe"
    Age = 35
    Department = "IT"
}
$user | Export-Clixml -Path "user_profile.xml"

This creates a custom user object and exports it. The XML file contains all properties with their values. The object type is preserved for import.

Exporting secure strings

Export-Clixml can securely export sensitive data like passwords. It uses Windows Data Protection API for encryption. Only the same user on the same machine can decrypt it. This is safer than plain text storage.

export4.ps1
$secureString = Read-Host -AsSecureString -Prompt "Enter password"
$secureString | Export-Clixml -Path "password.xml"

This securely stores a password prompt response. The data is encrypted using the current user's credentials. It can only be decrypted by the same user.

Comparing with Export-Csv

Unlike Export-Csv, Export-Clixml preserves object type and structure. CSV is plain text and loses type information. CLIXML is better for complex objects and later rehydration. CSV is better for portability.

export5.ps1
Get-Service | Select-Object -First 5 | Export-Clixml -Path "services.xml"
Get-Service | Select-Object -First 5 | Export-Csv -Path "services.csv"

This shows both export methods side by side. The XML file preserves the ServiceController objects. The CSV only contains property values as text.

Depth parameter for nested objects

The -Depth parameter controls how many levels of nested objects are serialized. Default is 2, which may truncate complex objects. Increase this for deeply nested structures. Higher values increase file size.

export6.ps1
$complexObject = Get-Process -Name "powershell" | Select-Object -Property *
$complexObject | Export-Clixml -Path "deep_process.xml" -Depth 5

This exports a PowerShell process with all properties. The increased depth ensures nested objects are fully serialized. The file will be larger but more complete.

Exporting for cross-session use

CLIXML files are primarily for PowerShell-to-PowerShell transfer. They can be used to save state between sessions. The files are machine-specific for some object types. They're not suitable for cross-platform sharing.

export7.ps1
$sessionData = @{
    Time = Get-Date
    User = $env:USERNAME
    Processes = Get-Process
}
$sessionData | Export-Clixml -Path "session_state.xml"

This exports various session information to a file. The data can be imported in another session. Some objects may not work across different machines.

Source

PowerShell documentation

This article covered the Export-Clixml cmdlet in PowerShell. We explored basic and advanced usage scenarios. The cmdlet is powerful for object serialization. It's particularly useful for PowerShell-specific data storage.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all PowerShell tutorials.