PowerShell Logo Small

Export-PSSession



This is the built-in help made by Microsoft for the command 'Export-PSSession', in PowerShell version 5 - as retrieved from Windows version 'Microsoft Windows Server 2012 R2 Standard' PowerShell help files on 2016-06-23.

For PowerShell version 3 and up, where you have Update-Help, this command was run just before creating the web pages from the help files.

SYNOPSIS

Imports commands from another session and saves them in a Windows PowerShell module.

SYNTAX


Export-PSSession [-Session] <PSSession> [-OutputModule] <String> [[-CommandName] [<String[]>]] [[-FormatTypeName] [<String[]>]] [-AllowClobber] [-ArgumentList [<Object[]>]]
[-Certificate [<X509Certificate2>]] [-CommandType {Alias | Function | Filter | Cmdlet | ExternalScript | Application | Script | Workflow | Configuration | All}] [-Encoding
{Unicode | UTF7 | UTF8 | ASCII | UTF32 | BigEndianUnicode | Default | OEM}] [-Force] [-FullyQualifiedModule [<ModuleSpecification[]>]] [-InformationAction {SilentlyContinue
| Stop | Continue | Inquire | Ignore | Suspend}] [-InformationVariable [<System.String>]] [-Module [<String[]>]] [<CommonParameters>]



Search powershellhelp.space

DESCRIPTION


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


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 the New-PSSession cmdlet 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 the CommandName parameters to specify
the commands to export.


The Export-PSSession cmdlet 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.



<

RELATED LINKS

Online Version: http://go.microsoft.com/fwlink/p/?linkid=293959
Import-Module
Import-PSSession
Invoke-Command
New-PSSession

REMARKS

<

Examples


-------------------------- EXAMPLE 1 --------------------------

PS C:\>$s = new-pssession -computerName Server01
PS C:\>export-pssession -session $s -outputModule Server01



The commands in this example export all commands from a PSSession on the Server01 computer to the Server01 module on the local computer except for commands that have the
same names as commands in the current session. It also exports the formatting data for the commands.

The first command creates a PSSession on the Server01 computer. The second command exports the commands and formatting data from the session into the Server01 module.










-------------------------- EXAMPLE 2 --------------------------

PS C:\>$s = new-pssession -ConnectionUri http://exchange.microsoft.com/mailbox -credential exchangeadmin01@hotmail.com -authentication negotiate
PS C:\>export-pssession -session $r -module exch* -commandname get-*, set-* -formattypename * -outputModule $pshome\Modules\Exchange -encoding ASCII



These commands export the Get and Set commands from a Microsoft Exchange Server snap-in on a remote computer to an Exchange module in the $pshome\Modules directory on the
local computer.

Placing the module in the $pshome\Module directory makes it accessible to all users of the computer.










-------------------------- EXAMPLE 3 --------------------------

PS C:\>$s = new-pssession -computerName Server01 -credential Server01\User01
PS C:\>export-pssession -session $s -outputModule TestCmdlets -type cmdlet -commandname *test* -formattypename *
PS C:\>remove-pssession $s
PS C:\>import-module TestCmdlets
PS C:\>get-help test*
PS C:\>test-files



These commands export cmdlets from a PSSession on a remote computer and save them in a module on the local computer. Then, the commands add the cmdlets from the module to
the current session so that they can be used.

The first command creates a PSSession on the Server01 computer and saves it in the $s variable.

The second command exports the cmdlets whose names begin with "Test" from the PSSession in $s to the TestCmdlets module on the local computer.

The third command uses the Remove-PSSession cmdlet to delete the PSSession in $s from the current session. This command shows that the PSSession need not be active to use
the commands that were imported from it.

The fourth command, which can be run in any session at any time, uses the Import-Module cmdlet to add the cmdlets in the TestCmdlets module to the current session.

The fifth command uses the Get-Help cmdlet to get help for cmdlets whose names begin with "Test." After the commands in a module are added to the current session, you can
use the Get-Help and Get-Command cmdlets to learn about the imported commands, just as you would use them for any command in the session.

The sixth command uses the Test-Files cmdlet, which was exported from the Server01 computer and added to the session.

Although it is not evident, the Test-Files command actually runs in a remote session on the computer from which the command was imported. Windows PowerShell creates a
session from information that is stored in the module.










-------------------------- EXAMPLE 4 --------------------------

PS C:\>export-pssession -session $s -AllowClobber -outputModule AllCommands



This command exports all commands and all formatting data from the PSSession in the $s variable into the current session. The command uses the AllowClobber parameter to
include commands with the same names as commands in the current session.










-------------------------- EXAMPLE 5 --------------------------

PS C:\>$options = New-PSSessionOption -NoMachineProfile
PS C:\>$s = new-pssession -computername Server01 -sessionoption $options
PS C:\>export-pssession -session $s -outputModule Server01
PS C:\>remove-pssession $s
PS C:\>new-pssession -computername Server01 -sessionoption $options
PS C:\>import-module Server01



This example shows how to run the exported commands in a session with particular options when the PSSession from which the commands were exported is closed.

When you use Export-PSSession, it saves information about the original PSSession in the module that it creates. When you import the module, if the original remote session is
closed, the module will use any open remote session that connects to originating computer.

If the current session does not include a remote session to the originating computer, the commands in the module will re-establish a session to that computer. However,
Export-PSSession does not save special options, such as those set by using the SessionOption parameter of New-PSSession, in the module.

Therefore, if you want to run the exported commands in a remote session with particular options, you need to create a remote session with the options that you want before
you import the module.

The first command uses the New-PSSessionOption cmdlet to create a PSSessionOption object, and it saves the object in the $options variable.

The second command creates a PSSession that includes the specified options. The command uses the New-PSSession cmdlet to create a PSSession on the Server01 computer. It uses
the SessionOption parameter to submit the option object in $options.

The third command uses the Export-PSSession cmdlet to export commands from the PSSession in $s to the Server01 module.

The fourth command uses the Remove-PSSession cmdlet to delete the PSSession in the $s variable.

The fifth command uses the New-PSSession cmdlet to create a new PSSession that connects to the Server01 computer. This PSSession also uses the session options in the
$options variable.

The sixth command uses the Import-Module cmdlet to import the commands from the Server01 module. The commands in the module run in the PSSession on the Server01 computer.