CmdID

CmdID / PowerShell / Cmdlets / Import

Import-PSSession

Imports commands from another session into the current session. PowerShell reference

From the PowerShell reference 11 parameters Alias ipsn

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

Imports commands from another session into the current session. The Import-PSSession cmdlet imports commands , such as cmdlets, functions, and aliases, from a PSSession on a local or remote computer into the current session. You can import any command that the Get-Command cmdlet can find in the PSSession. Use an Import-PSSession command to import commands from a customized shell, such as a Microsoft Exchange Server shell, or from a session that includes Windows PowerShell modules and snap-ins or other elements that are not in the current session. To import commands, first use the New-PSSession cmdlet to create a PSSession. Then, use the Import-PSSession cmdlet to import the commands. By default, Import-PSSession imports all commands except for commands that have the same names as commands in the current session. To import all the commands, use the AllowClobber parameter. You can use imported commands just as you would use any command in the session. When you use an imported command, the imported part of the command runs implicitly in the session from which it was imported. However, the remote operations are handled entirely by Windows PowerShell. You need not even be aware of them, except that you must keep the connection to the other session (PSSession) open. If you close it, the imported commands are no longer available. Because imported commands might take longer to run than local commands, Import-PSSession adds an AsJob parameter to every imported command. This parameter allows you to run the command as a Windows PowerShell background job. For more information, see about_Jobs. When you use Import-PSSession, Windows PowerShell adds the imported commands to a temporary module that exists only in your session and returns an object that represents the module. To create a persistent module that you can use in future sessions, use the Export-PSSession cmdlet. The Import-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. Beginning in Windows PowerShell 3.0, you can use the Import-Module cmdlet to import modules from a remote session into the current session. This feature uses implicit remoting. …

Import-PSSession [-Prefix <String>] [-DisableNameChecking] [[-CommandName] <String[]>] [-AllowClobber] [-ArgumentList <Object[]>] [-CommandType <CommandTypes>] [-Module <String[]>] [-FullyQualifiedModule <ModuleSpecification[]>] [[-FormatTypeName] <String[]>] [-Certificate <X509Certificate2>] [-Session] <PSSession> [<CommonParameters>]

Import all commands from a PSSession

$S = New-PSSession -ComputerName Server01 Import-PSSession -Session $SIdentify

Import commands that end with a specific string

$S = New-PSSession https://ps.testlabs.com/powershell Import-PSSession -Session $S -CommandName *-test -FormatTypeName * New-Test -Name Test1 Get-Test test1 | Run-TestIdentify

Import cmdlets from a PSSession

$S1 = New-PSSession -ComputerName s1 $S2 = New-PSSession -ComputerName s2 Import-PSSession -Session s1 -Type Cmdlet -Name New-Test, Get-Test -FormatTypeName * Import-PSSession -Session s2 -Type Cmdlet -Name Set-Test -FormatTypeName * New-Test Test1 | Set-Test -RunType FullIdentify

Run an imported command as a background job

$S = New-PSSession -ComputerName Server01 Import-PSSession -Session $S -CommandName *-test* -FormatTypeName * $batch = New-Test -Name Batch -AsJob Receive-Job $batchIdentify

Import cmdlets and functions from a Windows PowerShell module

$S = New-PSSession -ComputerName Server01 Invoke-Command -Session $S {Import-Module TestManagement} Import-PSSession -Session $S -Module TestManagementIdentify

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

Create a module in a temporary file

PS C:> Import-PSSession $S -CommandName Get-Date, SearchHelp -FormatTypeName * -AllowClobber Name : tmp_79468106-4e1d-4d90-af97-1154f9317239_tcw1zunz.ttf Path : C:\Users\User01\AppData\Local\Temp\tmp_79468106-4e1d-4d90-af97-1154f9317239_tcw1 zunz.ttf\tmp_79468106-4e1d-4d90-af97-1154f9317239_ tcw1zunz.ttf.psm1 Description : Implicit remoting for http://server01.corp.fabrikam.com/wsman Guid : 79468106-4e1d-4d90-af97-1154f9317239 Version : 1.0 ModuleBase : C:\Users\User01\AppData\Local\Temp\tmp_79468106-4e1d-4d90-af97-1154f9317239_tcw1 zunz.ttf ModuleType : Script PrivateData : {ImplicitRemoting} AccessMode : ReadWrite ExportedAliases : {} ExportedCmdlets : {} ExportedFunctions : {[Get-Date, Get-Date], [SearchHelp, SearchHelp]} ExportedVariables : {} NestedModules : {}Identify

Trips 1 Sigma rule: mediumPotentially Suspicious Powershell Script Execution From Temp Folder by Florian Roth (Nextron Systems), Max Altgelt (Nextron Systems), Tim Shelton

Run a command that is hidden by an imported command

PS C:> Import-PSSession $S -CommandName Get-Date -FormatTypeName * -AllowClobber PS C:> Get-Command Get-Date -All CommandType Name Definition ----------- ---- ---------- Function Get-Date ... Cmdlet Get-Date Get-Date [[-Date] <DateTime>] [-Year <Int32>] [-Month <Int32>] PS C:> Get-Date 09074 PS C:> (Get-Command -Type Cmdlet -Name Get-Date).PSSnapin.Name Microsoft.PowerShell.Utility PS C:> Microsoft.PowerShell.Utility\Get-Date Sunday, March 15, 2009 2:08:26 PMIdentify

Import commands that have a specific string in their names

PS C:> Import-PSSession -Session $S -CommandName Item -AllowClobberIdentify

Use the Module parameter to discover which commands were imported into the session

PS C:> $M = Import-PSSession -Session $S -CommandName bits -FormatTypeName bits PS C:> Get-Command -Module $M CommandType Name ----------- ---- Function Add-BitsFile Function Complete-BitsTransfer Function Get-BitsTransfer Function Remove-BitsTransfer Function Resume-BitsTransfer Function Set-BitsTransfer Function Start-BitsTransfer Function Suspend-BitsTransferIdentify

Trips 1 Sigma rule: mediumUsage Of Web Request Commands And Cmdlets by James Pemberton / @4A616D6573, Endgame, JHasenbusch, oscd.community, Austin Songer @austinsonger

Its page in the reference: Import-PSSession.

Parameters

11 in the PowerShell reference
-AllowClobberIndicates that this cmdlet imports 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, Import-PSSession does not import commands that have the same name as commands in the current session.
-ArgumentListSpecifies an array of commands that results from using the specified arguments (parameter values). For instance, to import the variant of the Get-Item command in the certificate (Cert:) drive in the PSSession in $S, type Import-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 temporary module that Import-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.
-CommandNameSpecifies commands with the specified names or name patterns. Wildcards are permitted. Use CommandName or its alias, Name. By default, Import-PSSession imports all commands from the session, 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 session. To import 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 imported unless you use the FormatTypeName parameter. Similarly, if you use the FormatTypeName parameter, no commands are imported unless you use the CommandName parameter.
-CommandTypeSpecifies the type of command objects. The default value is Cmdlet. Use CommandType or its alias, Type. The acceptable values for this parameter are: - Alias: The Windows PowerShell aliases in the remote session. - All: The cmdlets and functions in the remote session. - Application: All the files other than Windows-PowerShell files in the paths that are listed in the PATH environment variable ($Env:PATH) in the remote session, including .txt, .exe, and .dll files. - Cmdlet: The cmdlets in the remote session. "Cmdlet" is the default. - ExternalScript: The .ps1 files in the paths listed in the PATH environment variable ($Env:PATH) in the remote session. - Filter and Function: The Windows PowerShell functions in the remote session. - Script: The script files accessible in the remote session. 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. The cmdlet will combine the values using a binary-OR operation. …
-DisableNameCheckingIndicates that this cmdlet suppresses the message that warns you when you import a cmdlet or function whose name includes an unapproved verb or a prohibited character. By default, when a module that you import exports cmdlets or functions that have unapproved verbs in their names, the Windows PowerShell displays the following warning message: "WARNING: Some imported command names include unapproved verbs which might make them less discoverable. Use the Verbose parameter for more detail or type Get-Verb to see the list of approved verbs." This message is only a warning. The complete module is still imported, including the non-conforming commands. Although the message is displayed to module users, the naming problem should be fixed by the module author.
-FormatTypeNameSpecifies formatting instructions for the specified Microsoft .NET Framework types. Enter the type names. Wildcards are permitted. 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 the command does not include either the CommandName or FormatTypeName parameter, Import-PSSession imports formatting instructions for all .NET Framework types returned by a Get-FormatData command in the remote session. If you use the FormatTypeName parameter, no commands are imported unless you use the CommandName parameter. Similarly, if you use the CommandName parameter, the formatting files for the commands are not imported 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.
-ModuleSpecifies and array of commands in the Windows PowerShell snap-ins and modules. Enter the snap-in and module names. Wildcards are not permitted. Import-PSSession cannot import providers from a snap-in. For more information, see about_PSSnapins and about_Modules.
-PrefixSpecifies a prefix to the nouns in the names of imported commands. Use this parameter to avoid name conflicts that might occur when different commands in the session have the same name. For instance, if you specify the prefix Remote and then import a Get-Date cmdlet, the cmdlet is known in the session as Get-RemoteDate, and it is not confused with the original Get-Date cmdlet.
-SessionSpecifies the PSSession from which the cmdlets are imported. Enter a variable that contains a session object or a command that gets a session object, such as a New-PSSession or Get-PSSession command. You can specify only one session. This parameter is required.