Export Clixml - PowerShell


Overview

Export-Clixml exports PowerShell cmdlets and providers to a Common Language Infrastructure (CLI) assembly. This enables the cmdlets and providers to be used from non-PowerShell environments, such as C# or F#.

Syntax

Export-Clixml [[-InputObject] <PSSnapInInfo>] [-Namespace <string>] [-Path <string>] [-Force] [-Confirm] [-WhatIf] [-OutVariable <string>]

Options/Flags

-InputObject
Specifies the PSSnapInInfo object for the snap-in containing the cmdlets and providers to export.

-Namespace
Specifies the namespace for the exported cmdlets and providers. The default namespace is “PowerShell”.

-Path
Specifies the path to the file where the exported cmdlets and providers will be saved. The default path is the current directory.

-Force
Forces the export to overwrite an existing file without prompting for confirmation.

-Confirm
Prompts for confirmation before exporting the cmdlets and providers.

-WhatIf
Shows what would happen if the command were run without actually exporting the cmdlets and providers.

-OutVariable
Specifies a variable to store the exported cmdlets and providers.

Examples

Simple Example

This command exports the Get-Process cmdlet to a file named Process.clixml in the current directory:

Export-Clixml -InputObject Get-Process -Path Process.clixml

Complex Example

This command exports all cmdlets and providers from the Microsoft.PowerShell.Management snap-in to a file named Management.clixml in the C:\Exports directory:

Export-Clixml -InputObject (Get-PSSnapin -Name Microsoft.PowerShell.Management) -Path C:\Exports\Management.clixml

Common Issues

  • If the specified path is not valid or does not have write permissions, the export will fail.
  • If the specified namespace is already in use by another assembly, the export will fail.

Integration

The exported cmdlets and providers can be used from non-PowerShell environments using the System.Management.Automation namespace. For example, the following C# code uses the Get-Process cmdlet exported in the previous example:

using System.Management.Automation;

namespace CSharpExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PowerShell runspace.
            using (PowerShell runspace = PowerShell.Create())
            {
                // Add the exported assembly to the runspace.
                runspace.AddAssembly("Process.clixml");

                // Create a command to get the processes.
                PowerShellCommand command = new PowerShellCommand("Get-Process");

                // Execute the command.
                Collection<PSObject> results = runspace.Invoke(command);

                // Print the process names.
                foreach (PSObject result in results)
                {
                    Console.WriteLine(result.Members["Name"].Value);
                }
            }
        }
    }
}
  • Get-PSSnapin
  • Import-Clixml
  • New-PSSnapIn
  • Remove-PSSnapin