Import commands from another session and save them in a Windows PowerShell module.

      Export-PSSession [-Session] PSSession [-OutputModule] string 
         [[-CommandName] string[]] [[-FormatTypeName] string[]]
            [-AllowClobber] [-ArgumentList Object[]]
               [-CommandType {Alias | Function | Filter | Cmdlet | ExternalScript | Application | Script | All}]
                  [-Encoding string] [-Force] [-Module string[]] [CommonParameters]

       Export the specified commands, even if they have the same names as
       commands in the current session.

       If you import a command with the same name as a command in the current session,
       the imported command hides or replaces the original commands.
       For more information, see about_Command_Precedence. 

       By default Export-PSSession does not import commands that have the same names as commands
       in the current session. The default behavior is designed to prevent command name conflicts.

   -ArgumentList Object[]
       Export the variant of the command that results from using the specified arguments (parameter values). 
       For example, to export the variant of the Get-Item command in the certificate (Cert:) drive
       in the PSSession in $sess, type "export-pssession -session $sess -command Get-Item -argumentlist cert:".

    -CommandName string[]
       Export only the commands with the specified string names or name patterns.
       Wildcards are permitted.
       Use "-CommandName" or its alias, "-Name".

       By default, Export-PSSession will export all commands from the PSSession except
       for commands that have the same names as commands in the current session.
       This prevents imported commands from hiding or replacing commands in the
       current session. To export all commands, even those that hide or replace other commands,
       use -AllowClobber 
       If you use -CommandName, the formatting files for the commands are not exported unless
       you use -FormatTypeName. Similarly, if you use -FormatTypeName, no commands are exported unless 
       you specify -CommandName.

    -CommandType CommandTypes
        Export only specific types of command objects. Use "-CommandType" or its alias, "-Type".

        Valid values are:
           Alias           All PowerShell aliases in the current session.
           All             All command types. It is the equivalent of "get-command *".
           Application     All files other than  PowerShell files in paths listed in the Path environment variable 
                           ($env:path), including .txt, .exe, and .dll files.
           Cmdlet          The cmdlets in the current session. "Cmdlet" is the default.
           ExternalScript  All .ps1 files in the path(s) listed in the Path environment variable ($env:path).
           Filter          All PowerShell functions.
           Function        All PowerShell functions.
           Script          Script blocks in the current session.

   -Encoding string
       The encoding for the output files. Valid values are "Unicode", "UTF7", "UTF8", "ASCII", "UTF32", "Big
       EndianUnicode", "Default", and "OEM". The default is "UTF-8".

       Overwrite one or more existing output files, even if the file has the read-only attribute.

   -FormatTypeName string[]
       Export formatting instructions only for the specified Microsoft .NET Framework types.
       Enter the type names. By default, Export-PSSession exports formatting instructions for all
       .NET Framework types that are not in the System.Management.Automation namespace.
       The value of this parameter must be the name of a type that is returned by a Get-FormatData
       command in the session from which the commands are being imported.

       To get all of the formatting data in the remote session, type *

       If you use the FormatTypeName parameter, no commands are exported unless you use -CommandName.
       Similarly, if you use -CommandName, the formatting files for the commands are not exported unless 
       you also use -FormatTypeName.

   -Module string[]
       Export only the commands in the specified PowerShell snap-ins and modules.
       Enter the snap-in and module names. Wildcards are not permitted.
       For more information, see about_PSSnapins and Import-Module.

   -OutputModule string
       A path (optional) and name for the module that Export-PSSession creates.
       The default path is $home\Documents\WindowsPowerShell\Modules.
       This parameter is required.
       If the module subdirectory or any of the files that Export-PSSession creates already exist,
       the command fails. 
       To overwrite existing files, use -Force.

   -Session PSSession
       The PSSession from which the commands are exported.
       Enter a variable that contains a session object or a command that gets a session object,
       such as a Get-PSSession command. This parameter is required.

Standard Aliases for Export-PSSession: epsn

Under Windows, Export-PSSession will create UTF-8 files by default.
This can be configured via the -Encoding option or by setting the $PSDefaultParameterValues preference variable.
Under PowerShell Core edition, the encoding defaults to BOM-less UTF-8

Export-PSSession gets cmdlets, functions, aliases, and other command types from another PSSession on a local or remote computer and saves them in a PowerShell module. To add the commands from the module to the current session, use Import-Module.

Unlike Import-PSSession, which imports commands from another PSSession into the current session, Export-PSSession saves the commands in a module. The commands are not imported into the current session.

To export commands, first use New-PSSession to create a PSSession that has the commands that you want to export. Then use the Export-PSSession cmdlet to export the commands. By default, Export-PSSession exports all commands, except for commands that exist in the current session, but you can use -CommandName to specify the commands to export.

Export-PSSession uses the implicit remoting feature of Windows PowerShell. When you import commands into the current session, they run implicitly in the original session or in a similar session on the originating computer. If the original remote session is closed, then accessing the module will automatically re-open a remote session connected to the originating computer.


Export all commands from a PSSession on the Server64 computer to the Server64 module on the local computer. Include formatting data but exclude any commands that have the same names as commands in the current session:

PS C:> $sess = New-PSSession -computerName Server64
PS C:> Export-PSSession -session $sess -outputModule Server64

Export all commands and all formatting data from the PSSession in the $sess variable into the current session.:

PS C:> Export-PSSession -session $sess -AllowClobber -outputModule AllCommands

Note that if Enter-PSSession -ComputerName is used (instead of new-psSession), then Enter-PSSession would have automatically created a temporary session that would close as soon as the interactive session ends.

“Laughter is America’s most important export” ~ Walt Disney

Related PowerShell Cmdlets

Import-PSSession - Import commands from another session.

Copyright © 1999-2024 SS64.com
Some rights reserved