PowerShell Import-Clixml
last modified February 15, 2025
In this article, we will cover the Import-Clixml
cmdlet in
PowerShell. This cmdlet imports objects from CLIXML files created with
Export-Clixml
.
CLIXML basics
CLIXML is PowerShell's serialization format for objects. It preserves object
structure and properties when saving to files. Import-Clixml
reconstructs objects from these files. This is useful for data persistence
and transfer between sessions.
Basic Import-Clixml usage
The simplest way to use Import-Clixml
is with a file path. This
reads the CLIXML file and recreates the original objects. The objects maintain
their properties and methods. This example shows basic file import.
$data = Import-Clixml -Path "C:\data\processes.clixml" $data
This command imports objects from processes.clixml into $data. The objects can then be used like any other PowerShell objects. The file must exist.
Importing process objects
You can save and restore process information using CLIXML. First export
processes with Export-Clixml
, then import them later. This
preserves the process objects' state at export time. Useful for analysis.
Get-Process | Export-Clixml -Path "C:\temp\processes.clixml" $savedProcesses = Import-Clixml -Path "C:\temp\processes.clixml" $savedProcesses | Select-Object Name, Id, CPU -First 5
This exports current processes to a file, then imports them. The Select-Object cmdlet shows name, ID, and CPU for the first 5 processes. Note that live process data may differ from saved state.
Importing custom objects
Import-Clixml
works with custom objects too. Create a custom
object, export it, then import it in another session. All properties are
preserved. This enables complex data persistence scenarios.
$customObj = [PSCustomObject]@{ Name = "Server01" Status = "Online" Uptime = (Get-Date).AddDays(-1) } $customObj | Export-Clixml -Path "C:\temp\server.clixml" $importedObj = Import-Clixml -Path "C:\temp\server.clixml" $importedObj
This creates a custom server object, exports it, then imports it. The imported object has all original properties. DateTime objects are properly reconstructed.
Importing secure strings
Import-Clixml
can securely import encrypted strings. When used
with Export-Clixml
, it maintains encryption. This is useful for
storing credentials securely. The encryption uses Windows Data Protection API.
$secureString = Read-Host "Enter password" -AsSecureString $secureString | Export-Clixml -Path "C:\temp\password.clixml" $importedSecureString = Import-Clixml -Path "C:\temp\password.clixml" $credential = New-Object System.Management.Automation.PSCredential("user", $importedSecureString)
This securely saves a password, then recreates a credential object. The password remains encrypted on disk and in memory. Only the same user on the same machine can decrypt it.
Importing from pipeline
Import-Clixml
can accept input from the pipeline. This allows
chaining with other cmdlets. The Path parameter accepts pipeline input by
property name. Useful for dynamic file handling.
Get-ChildItem "C:\data\*.clixml" | Import-Clixml
This imports all CLIXML files in C:\data. Each file's contents are output to the pipeline. Note that all files must contain valid CLIXML data.
Error handling
When importing CLIXML files, errors may occur. Common issues include invalid paths or corrupted data. Use try/catch blocks to handle errors gracefully. This example shows robust import handling.
try { $data = Import-Clixml -Path "C:\data\missing.clixml" -ErrorAction Stop $data } catch { Write-Warning "Failed to import CLIXML: $_" }
This attempts to import a potentially missing file. The -ErrorAction Stop ensures exceptions are caught. The catch block handles any import errors.
Importing complex objects
Import-Clixml
can handle complex object hierarchies. This
includes arrays, hashtables, and nested objects. The entire object graph
is preserved. This example demonstrates nested object import.
$complexObject = @{ Servers = @( [PSCustomObject]@{Name="Web01"; Role="Frontend"}, [PSCustomObject]@{Name="DB01"; Role="Backend"} ) Timestamp = Get-Date } $complexObject | Export-Clixml -Path "C:\temp\servers.clixml" $imported = Import-Clixml -Path "C:\temp\servers.clixml" $imported.Servers | Format-Table
This creates and exports a complex object with nested arrays. The import recreates the full structure. The Format-Table cmdlet displays the servers.
Source
In this article, we have covered the Import-Clixml cmdlet in PowerShell.
Author
List all PowerShell tutorials.