CmdID

CmdID / PowerShell / Cmdlets / Export

Export-PSSession

Exports commands from another session and saves them in a PowerShell module. PowerShell reference

From the PowerShell reference 12 parameters Alias epsn

Opens CmdID with your command, read as PowerShell. It is read in your browser and sent nowhere.

What it does

from the PowerShell reference, CC BY 4.0

Exports commands from another session and saves them in a PowerShell module. The Export-PSSession cmdlet gets cmdlets, functions, aliases, and other command types from another PowerShell session (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 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, 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. To prevent command name conflicts, the default for Export-PSSession is to export all commands, except for commands that exist in the current session. You can use the CommandName parameter to specify the commands to export. The Export-PSSession cmdlet uses the implicit remoting feature of 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.

Export-PSSession [-Session] <PSSession> [-OutputModule] <string> [[-CommandName] <string[]>] [[-FormatTypeName] <string[]>] [-Force] [-Encoding <string>] [-AllowClobber] [-ArgumentList <Object[]>] [-CommandType <CommandTypes>] [-Module <string[]>] [-FullyQualifiedModule <ModuleSpecification[]>] [-Certificate <X509Certificate2>] [<CommonParameters>]

Export commands from a PSSession

$S = New-PSSession -ComputerName Server01 Export-PSSession -Session $S -OutputModule Server01Identify

Export the Get and Set commands

$credential = Get-Credential -UserName 'exchangeadmin01@contoso.com' -Message 'Enter Exchange credentials' $newSession = @{ ConnectionUri = 'https://exchange.microsoft.com/mailbox' Credential = $credential Authentication = 'Negotiate' } $S = New-PSSession @newSession $exportSession = @{ Session = $S Module = 'exch*' CommandName = 'Get-*', 'Set-*' FormatTypeName = '*' OutputModule = "$PSHOME\Modules\Exchange" Encoding = 'ascii' } Export-PSSession @exportSessionIdentify

Export commands from a remote computer

$credential = Get-Credential -UserName 'Server01\User01' -Message 'Enter credentials for Server01' $newSession = @{ ComputerName = 'Server01' Credential = $credential } $S = New-PSSession @newSession $exportSession = @{ Session = $S OutputModule = 'TestCmdlets' Type = 'Cmdlet' CommandName = 'test' FormatTypeName = '*' } Export-PSSession @exportSession Remove-PSSession $S Import-Module TestCmdlets Get-Help Test* Test-FilesIdentify

Trips 1 Sigma rule: lowImport New Module Via PowerShell CommandLine by Nasreddine Bencherchali (Nextron Systems)

Export and clobber commands in the current session

Export-PSSession -Session $S -AllowClobber -OutputModule AllCommandsIdentify

Export commands from a closed PSSession

$Options = New-PSSessionOption -NoMachineProfile $S = New-PSSession -ComputerName Server01 -SessionOption $Options Export-PSSession -Session $S -OutputModule Server01 Remove-PSSession $S New-PSSession -ComputerName Server01 -SessionOption $Options Import-Module Server01Identify

Trips 1 Sigma rule: lowImport New Module Via PowerShell CommandLine by Nasreddine Bencherchali (Nextron Systems)

Its page in the reference: Export-PSSession.

Parameters

12 in the PowerShell reference
-AllowClobberExports the specified commands, even if they have the same names as commands in the current session. If you export a command with the same name as a command in the current session, the exported command hides or replaces the original commands. For more information, see about_Command_Precedence.
-ArgumentListExports 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:.
-CertificateSpecifies the client certificate that is used to sign the format files (*.Format.ps1xml) or script module files (.psm1) in the module that Export-PSSession creates. Enter a variable that contains a certificate or a command or expression that gets the certificate. To find a certificate, use the Get-PfxCertificate cmdlet or use the Get-ChildItem cmdlet in the Certificate (Cert:) drive. If the certificate is not valid or does not have sufficient authority, the command fails.
-CommandNameExports 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 commands from being hidden or replaced by 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.
-CommandTypeExports only the specified types of command objects. Use CommandType or its alias, Type. The acceptable values for this parameter are as follows: - Alias: All PowerShell aliases in the current session. - All: All command types. It is the equivalent of Get-Command -Name *. - 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. - Configuration: A PowerShell configuration. For more information, see about_Session_Configurations. - ExternalScript: All .ps1 files in the paths listed in the PATH environment variable ($Env:PATH). - Filter and Function: All PowerShell functions. - Script Script files accessible in the current session. - Workflow A PowerShell workflow. For more information, see about_Workflows. These values are defined as a flag-based enumeration. You can combine multiple values together to set multiple flags using this parameter. The values can be passed to the CommandType parameter as an array of values or as a comma-separated string of those values. …
-EncodingSpecifies the type of encoding for the target file. The default value is UTF8. The acceptable values for this parameter are as follows: - ASCII: Uses ASCII (7-bit) character set. - BigEndianUnicode: Uses UTF-16 with the big-endian byte order. - Default; Uses the encoding that corresponds to the system's active code page. - OEM: Uses the encoding that corresponds to the system's current OEM code page. - Unicode: Uses UTF-16 with the little-endian byte order. - UTF7: Uses UTF-7. - UTF8: Uses UTF-8. - UTF32: Uses UTF-32 with the little-endian byte order.
-ForceOverwrites one or more existing output files, even if the file has the read-only attribute.
-FormatTypeNameExports 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. If you use the CommandName parameter, the formatting files for the commands are not exported unless you use the FormatTypeName parameter.
-FullyQualifiedModuleThe value can be a module name, a full module specification, or a path to a module file. When the value is a path, the path can be fully qualified or relative. A relative path is resolved relative to the script that contains the using statement. When the value is a name or module specification, PowerShell searches the PSModulePath for the specified module. A module specification is a hashtable that has the following keys. - ModuleName - Required Specifies the module name. - GUID - Optional Specifies the GUID of the module. - It's also Required to specify at least one of the three below keys. - ModuleVersion - Specifies a minimum acceptable version of the module. - MaximumVersion - Specifies the maximum acceptable version of the module. - RequiredVersion - Specifies an exact, required version of the module. This can't be used with the other Version keys. You can't specify the FullyQualifiedModule parameter in the same command as a Module parameter. the two parameters are mutually exclusive.
-ModuleExports 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 Import-Module and about_PSSnapins.
-OutputModuleSpecifies an optional path and name for the module created by Export-PSSession. 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.
-SessionSpecifies 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.