PowerShell Logo Small

Export-PSSession



This is the built-in help made by Microsoft for the command 'Export-PSSession', in PowerShell version 2 - as retrieved from Windows version 'Microsoft® Windows Vista™ Ultimate ' 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] [-Arg
umentList <Object[]>] [-CommandType {Alias | Function | Filter | Cmdlet | ExternalScript | Application | Script | All}] [-Encoding <string>] [-Fo
rce] [-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 sa
ves 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-P
SSession 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/?LinkID=135213
about_Command_Precedence
Import-PSSession
New-PSSession
Import-Module
Invoke-Command
about_PSSessions

REMARKS

<

Examples


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

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

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



Description
-----------
The commands in this example export all commands from a PSSession on the Server01 computer to the Server01 module on the local computer except fo
r 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 --------------------------

C:\PS>$s = new-pssession -ConnectionUri http://exchange.microsoft.com/mailbox -credential exchangeadmin01@hotmail.com -authentication negotiate

C:\PS> export-pssession -session $r -module exch* -commandname get-*, set-* -formattypename * -outputModule $pshome\Modules\Exchange -encoding AS
CII



Description
-----------
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 --------------------------

C:\PS>$s = new-pssession -computerName Server01 -credential Server01\User01

C:\PS> export-pssession -session $s -outputModule TestCmdlets -type cmdlet -commandname *test* -formattypename *

C:\PS> remove-pssession $s

C:\PS> import-module TestCmdlets

C:\PS> get-help test*

C:\PS> test-files



Description
-----------
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 c
mdlets 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 th
e 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 comma
nd 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 --------------------------

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



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








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

C:\PS>$options = New-PSSessionOption -NoMachineProfile

C:\PS> $s = new-pssession -computername Server01 -sessionoption $options

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

C:\PS> remove-pssession $s

C:\PS> new-pssession -computername Server01 -sessionoption $options

C:\PS> import-module Server01



Description
-----------
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 th
e 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 t
hat computer. However, Export-PSSession does not save special options, such as those set by using the SessionOption parameter of New-PSSession, i
n 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 opt
ions 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 th
e 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 se
ssion 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.