Export-PSSession - PowerShell command help and examples

Imports commands from another session and saves them in a Windows PowerShell module. (Export-PSSession)


NAME
Export-PSSession
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[]>] [-CommandType {Alias | Function | Filter | Cmdlet | ExternalScript | Application | Script | All}] [-Encoding <string>] [-Force] [-Module <string[]>] [<CommonParameters>]
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.
PARAMETERS
-AllowClobber [<SwitchParameter>] Exports 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. 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. Required? false Position? named Default value False Accept pipeline input? false Accept wildcard characters? false -ArgumentList <Object[]> Exports 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 $s, type "export-pssession -session $s -command get-item -argumentlist cert:". Required? false Position? named Default value None Accept pipeline input? false Accept wildcard characters? false -CommandName <string[]> Exports only the commands with the specified names or name patterns. Wildcards are permitted. Use "CommandName" or its alias, "Name". By default, Export-PSSession exports 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 the AllowClobber parameter. If you use the CommandName parameter, the formatting files for the commands are not exported unless you use the FormatTypeName parameter. Similarly, if you use the FormatTypeName parameter, no commands are exported unless you use the CommandName parameter. Required? false Position? 3 Default value All commands in the session. Accept pipeline input? false Accept wildcard characters? true -CommandType <CommandTypes> Exports only the specified types of command objects. Use "CommandType" or its alias, "Type". Valid values are: -- Alias: All Windows PowerShell aliases in the current session. -- All: All command types. It is the equivalent of "get-command *". -- Application: All files other than Windows 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 paths listed in the Path environment variable ($env:path). -- Filter and Function: All Windows PowerShell functions. -- Script: Script blocks in the current session. Required? false Position? named Default value All commands in the session. Accept pipeline input? false Accept wildcard characters? false -Encoding <string> Specifies the encoding for the output files. Valid values are "Unicode", "UTF7", "UTF8", "ASCII", "UTF32", "BigEndianUnicode", "Default", and "OEM". The default is "UTF-8". Required? false Position? named Default value UTF-8 Accept pipeline input? false Accept wildcard characters? false -Force [<SwitchParameter>] Overwrites one or more existing output files, even if the file has the read-only attribute. Required? false Position? named Default value False Accept pipeline input? false Accept wildcard characters? false -FormatTypeName <string[]> Exports 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 the CommandName parameter. Similarly, if you use the CommandName parameter, the formatting files for the commands are not exported unless you use the FormatTypeName parameter. Required? false Position? 4 Default value Accept pipeline input? false Accept wildcard characters? false -Module <string[]> Exports only the commands in the specified Windows 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. Required? false Position? named Default value All commands in the session. Accept pipeline input? false Accept wildcard characters? false -OutputModule <string> Specifies 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 the Force parameter. Required? true Position? 2 Default value Accept pipeline input? false Accept wildcard characters? false -Session <PSSession> Specifies 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. Required? true Position? 1 Default value None Accept pipeline input? false Accept wildcard characters? false <CommonParameters> This cmdlet supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer and OutVariable. For more information, type, "get-help about_commonparameters".
INPUTS
None You cannot pipe objects to Export-PSSession.
OUTPUTS
System.IO.FileInfo Export-PSSession returns a list of files that comprise the module that it created.
NOTES
Export-PSSession relies on the Windows PowerShell remoting infrastructure. To use this cmdlet, the computer must be configured for remoting. For more information, see about_Remote_Requirements. You cannot use Export-PSSession to export a Windows PowerShell provider. Exported commands run implicitly in the PSSession from which they were exported. However, the details of running the commands remotely are handled entirely by Windows PowerShell. You can run the exported commands just as you would run local commands. Export-Module captures and saves information about the PSSession in the module that it exports. If the PSSession from which the commands were exported is closed when you import the module, and there are no active PSSessions to the same computer, the commands in the module attempt to re-create the PSSession. If attempts to re-create the PSSession fail, the exported commands will not run. The session information that Export-Module captures and saves in the module does not include session options, such as those that you specify in the $PSSessionOption automatic variable or by using the SessionOption parameters of the New-PSSession, Enter-PSSession, or Invoke-Command cmdlet. If the original PSSession is closed when you import the module, the module will use another PSSession to the same computer, if one is available. To enable the imported commands to run in a correctly configured session, create a PSSession with the options that you want before you import the module. To find the commands to export, Export-PSSession uses the Invoke-Command cmdlet to run a Get-Command command in the PSSession. To get and save formatting data for the commands, it uses the Get-FormatData and Export-FormatData cmdlets. You might see error messages from Invoke-Command, Get-Command, Get-FormatData, and Export-FormatData when you run an Export-PSSession command. Also, Export-PSSession cannot export commands from a session that does not include the Get-Command, Get-FormatData, Select-Object, and Get-Help cmdlets. Export-PSSession uses the Write-Progress cmdlet to display the progress of the command. You might see the progress bar while the command is running. Exported commands have the same limitations as other remote commands, including the inability to start a program with a user interface, such as Notepad. Because Windows PowerShell profiles are not run in PSSessions, the commands that a profile adds to a session are not available to Export-PSSession. To export commands from a profile, use an Invoke-Command command to run the profile in the PSSession manually before exporting commands. The module that Export-PSSession creates might include a formatting file, even if the command does not import formatting data. If the command does not import formatting data, any formatting files that are created will not contain formatting data.

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 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
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 ASCII
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 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
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 AllowClobber 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 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. RELATED LINKS Online version: http://go.microsoft.com/fwlink/?LinkID=135213 about_Command_Precedence Import-PSSession New-PSSession Import-Module Invoke-Command about_PSSessions C:\Windows>powershell get-help Import-PSSession -full

Microsoft Windows [Version 10.0.19045.3693]
Copyright (c) 2023 Microsoft Corporation.

ColorConsole [Version 3.7.1000] PowerShell 2.0-Export

Windows 11, 10, 8.1, 8, 7 / Server 2022, 2019, 2016











Windows-10


... Windows 10 FAQ
... Windows 10 How To


Windows 10 How To


... Windows 11 How To
... Windows 10 FAQ



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

HTTP: ... PS_Windows/en/Export-PSSession.htm
0.046
18288

The mouse wheel settings Windows 11, 10, ... etc!

Das Verzeichnis in der Registerkarte der Eingabeaufforderung ändern!

Spenden über PayPal - Problem, warum?

High CPU usage by Windows Explorer at 100 percent?

Wofür sind die Linien und Schaltflächen an der Wurzel?

What I miss is being able to disconnect or connect the network drive?



(0)