Category Archives: PSSession

Remove-PSSession

NAME
    Remove-PSSession

SYNOPSIS
    Closes one or more Windows PowerShell sessions (PSSessions).

SYNTAX
    Remove-PSSession [[-ComputerName] <string[]>] [-Confirm] [-WhatIf] [<CommonParameters>]

    Remove-PSSession [-Id] <Int32[]> [-Confirm] [-WhatIf] [<CommonParameters>]

    Remove-PSSession [-InstanceId <Guid[]>] [-Confirm] [-WhatIf] [<CommonParameters>]

    Remove-PSSession [-Name <string[]>] [-Confirm] [-WhatIf] [<CommonParameters>]

    Remove-PSSession [-Session] <PSSession[]> [-Confirm] [-WhatIf] [<CommonParameters>]

DESCRIPTION
    The Remove-PSSession cmdlet closes Windows PowerShell sessions (PSSessions) in the current session. It stops any commands that are running in the PSSessions, ends the PSSession, and releases the resources that the PSSession was using. If the PSSession is connected to a remote computer, Remove-PSSession also closes the connection between the local and remote computers.

    To remove a PSSession, enter the Name, ComputerName, ID, or InstanceID of the session.

    If you have saved the PSSession in a Variable, the session object remains in the Variable, but the state of the PSSession is “Closed.”

PARAMETERS
    -ComputerName <string[]>
        Closes the PSSessions that are connected to the specified computers. Wildcards are permitted.

        Type the NetBIOS name, an IP address, or a fully qualified domain name of one or more remote computers. To specify the local computer, type the computer name, “localhost”, or a dot (.).

        Required?                    false
        Position?                    1
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? true

    -Id <Int32[]>
        Closes the PSSessions with the specified IDs. Type one or more IDs (separated by commas) or use the range operator (..) to specify a range of IDs

        An ID is an integer that uniquely identifies the PSSession in the current session. It is easier to remember and type than the InstanceId, but it is unique only within the current session. To find the ID of a PSSession, use Get-PSSession without parameters.

        Required?                    true
        Position?                    1
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -InstanceId <Guid[]>
        Closes the PSSessions with the specified instance IDs.

        The instance ID is a GUID that uniquely identifies a PSSession in the current session. The InstanceID is unique, even when you have multiple sessions running on a single computer.

        The InstanceID is stored in the InstanceID property of the object that represents a PSSession. To find the InstanceID of the PSSessions in the current session, type “Get-PSSession | Format-Table Name, ComputerName, InstanceId”.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -Name <string[]>
        Closes the PSSessions with the specified friendly names. Wildcards are permitted.

        Because the friendly name of a PSSession might not be unique, when using the Name parameter, consider also using the WhatIf or Confirm parameter in the Remove-PSSession command.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? true

    -Session <PSSession[]>
        Specifies the session objects of the PSSessions to close. Enter a Variable that contains the PSSessions or a command that creates or gets the PSSessions, such as a New-PSSession or Get-PSSession command. You can also pipe one or more session objects to Remove-PSSession.

        Required?                    true
        Position?                    1
        Default value
        Accept pipeline input?     true (ByValue, ByPropertyName)
        Accept wildcard characters? false

    -Confirm [<SwitchParameter>]
        Prompts you for confirmation before executing the command.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -WhatIf [<SwitchParameter>]
        Describes what would happen if you executed the command without actually executing the command.

        Required?                    false
        Position?                    named
        Default value
        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
    System.Management.Automation.Runspaces.PSSession
        You can pipe a session object to Remove-PSSession.

OUTPUTS
    None
        Remove-PSSession does not return any objects.

NOTES

        The ID parameter is mandatory. You cannot type “Remove-PSSession” without parameters. To delete all the PSSessions in the current session, type “Get-PSSession | Remove-PSSession“.

        A PSSession uses a persistent connection to a remote computer. Create a PSSession to run a series of commands that share data. For more information, see about_pssessions.

        PSSessions are specific to the current session. When you end a session, the PSSessions that you created in that session are forcibly closed.

    ————————– EXAMPLE 1 ————————–

    C:\PS>Remove-PSSession -Id 1, 2

    Description
    ———–
    This command removes the PSSessions that have IDs 1 and 2.

    ————————– EXAMPLE 2 ————————–

    C:\PS>Get-PSSession | Remove-PSSession

    C:\PS> Remove-PSSession -session (Get-PSSession)

    C:\PS> $s = Get-PSSession
    C:\PS> Remove-PSSession -session $s”.

    Description
    ———–
    These commands remove all of the PSSessions in the current session. Although the three command formats look different, they have the same effect.

    ————————– EXAMPLE 3 ————————–

    C:\PS>$r = Get-PSSession -computername Serv*

    $r | Remove-PSSession

    Description
    ———–
    These commands close the PSSessions that are connected to computers that have names that begin with “Serv”.

    ————————– EXAMPLE 4 ————————–

    C:\PS>Get-PSSession | where {$_.port -eq 90} | Remove-PSSession

    Description
    ———–
    This command closes the PSSessions that are connected to port 90. You can use this command format to identify PSSessions by properties other than ComputerName, Name, InstanceID, and ID.

    ————————– EXAMPLE 5 ————————–

    C:\PS>Get-PSSession | ft computername, instanceID -auto

    ComputerName InstanceId
    ———— —————-
    Server01     875d231b-2788-4f36-9f67-2e50d63bb82a
    localhost    c065ffa0-02c4-406e-84a3-dacb0d677868
    Server02     4699cdbc-61d5-4e0d-b916-84f82ebede1f
    Server03     4e5a3245-4c63-43e4-88d0-a7798bfc2414
    TX-TEST-01 fc4e9dfa-f246-452d-9fa3-1adbdd64ae85

    C:\PS> Remove-PSSession -InstanceID fc4e9dfa-f246-452d-9fa3-1adbdd64ae85

    Description
    ———–
    These commands show how to close a PSSession based on its instance ID (RemoteRunspaceID).

    The first command uses the Get-PSSession cmdlet to get the PSSessions in the current session. It uses a pipeline operator (|) to send the PSSessions to the Format-Table cmdlet (alias: ft), which formats their ComputerName and InstanceID properties in a table. The AutoSize parameter (“auto”) compresses the columns for display.

    From the resulting display, the administrator can identify the PSSession to be closed, and copy and paste the InstanceID of that PSSession into the second command.

    The second command uses the Remove-PSSession cmdlet to remove the PSSession with the specified instance ID.

    ————————– EXAMPLE 6 ————————–

    C:\PS>function EndPSS { Get-PSSession | Remove-PSSession }

    Description
    ———–
    This Function deletes all of the PSSessions in the current session. After you add this Function to your Windows Powershell profile, to delete all sessions, just type “endpss”.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=135250
    about_pssessions
    about_remote
    New-PSSession
    Get-PSSession
    Enter-PSSession
    Exit-PSSession
    Invoke-Command

New-PSSession

NAME
    New-PSSession

SYNOPSIS
    Creates a persistent connection to a local or remote computer.

SYNTAX
    New-PSSession [[-ComputerName] <string[]>] [-ApplicationName <string>] [-Authentication {Default | Basic | Negotiate | NegotiateWithImplicitCredential | Credssp | Digest | Kerberos}] [-CertificateThumbprint <string>] [-ConfigurationName <string>] [-Credential <PSCredential>] [-Name <string[]>] [-Port <int>] [-SessionOption <PSSessionOption>] [-UseSSL] [-ThrottleLimit <int>] [<CommonParameters>]

    New-PSSession [[-Session] <PSSession[]>] [-Name <string[]>] [-ThrottleLimit <int>] [<CommonParameters>]

    New-PSSession [-ConnectionURI] <Uri[]> [-AllowRedirection] [-Authentication {Default | Basic | Negotiate | NegotiateWithImplicitCredential | Credssp | Digest | Kerberos}] [-CertificateThumbprint <string>] [-ConfigurationName <string>] [-Credential <PSCredential>] [-Name <string[]>] [-SessionOption <PSSessionOption>] [-ThrottleLimit <int>] [<CommonParameters>]

DESCRIPTION
    The New-PSSession cmdlet creates a Windows PowerShell session (PSSession) on a local or remote computer. When you create a PSSession, Windows PowerShell establishes a persistent connection to the remote computer.

    Use a PSSession to run multiple commands that share data, such as a Function or the value of a Variable. To run commands in a PSSession, use the Invoke-Command cmdlet. To use the PSSession to interact directly with a remote computer, use the Enter-PSSession cmdlet. For more information, see about_pssessions.

    You can run commands on a remote computer without creating a PSSession by using the ComputerName parameters of Enter-PSSession or Invoke-Command. When you use the ComputerName parameter, Windows PowerShell creates a temporary connection that is used for the interactive session or for a single command and is then closed.

PARAMETERS
    -AllowRedirection [<SwitchParameter>]
        Allows redirection of this connection to an alternate Uniform Resource Identifier (URI).

        When you use the ConnectionURI parameter, the remote destination can return an instruction to redirect to a different URI. By default, Windows PowerShell does not redirect connections, but you can use the AllowRedirection parameter to allow it to redirect the connection.

        You can also limit the number of times that the connection is redirected by setting the MaximumConnectionRedirectionCount property of the $PSSessionOption preference Variable, or the MaximumConnectionRedirectionCount property of the value of the SessionOption parameter.
        The default value is 5. For more information, see the description of the SessionOption parameter, and see New-PSSessionOption.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -ApplicationName <string>
        Specifies the application name segment of the connection URI. Use this parameter to specify the application name when you are not using the ConnectionURI parameter in the command.

        The default value is the value of the $PSSessionApplicationName preference Variable on the local computer. If this preference Variable is not defined, the default value is “WSMAN”. This value is appropriate for most uses. For more information, see about_preference_variables.

        The WinRM service uses the application name to select a listener to service the connection request. The value of this parameter should match the value of the URLPrefix property of a listener on the remote computer.

        Required?                    false
        Position?                    named
        Default value                WSMan
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -Authentication <AuthenticationMechanism>
        Specifies the mechanism that is used to authenticate the user’s credentials. Valid values are “Default”, “Basic”, “Credssp”, “Digest”, “Kerberos”, “Negotiate”, and “NegotiateWithImplicitCredential”. The default value is “Default”.

        CredSSP authentication is available only in Windows Vista, Windows Server 2008, and later versions of Windows.

        For more information about the values of this parameter, see the description of the System.Management.Automation.Runspaces.AuthenticationMechanism enumeration in the MSDN (Microsoft Developer Network) library at http://go.microsoft.com/fwlink/?LinkID=144382.

        Caution: Credential Security Service Provider (CredSSP) authentication, in which the user’s credentials are passed to a remote computer to be authenticated, is designed for commands that require authentication on more than one resource, such as accessing a remote network share. This mechanism increases the security risk of the remote operation. If the remote computer is compromised, the credentials that are passed to it can be used to control the network session.

        Required?                    false
        Position?                    named
        Default value                Default
        Accept pipeline input?     false
        Accept wildcard characters? false

    -CertificateThumbprint <string>
        Specifies the digital public key Certificate (X509) of a user account that has permission to perform this action. Enter the Certificate thumbprint of the Certificate.

        Certificates are used in client Certificate-based authentication. They can be mapped only to local user accounts; they do not work with domain accounts.

        To get a Certificate thumbprint, use the Get-Item or Get-ChildItem command in the Windows PowerShell Cert: drive.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -ComputerName <string[]>
        Creates a persistent connection (PSSession) to the specified computer. If you enter multiple computer names, New-PSSession creates multiple PSSessions, one for each computer. The default is the local computer.

        Type the NetBIOS name, an IP address, or a fully qualified domain name of one or more remote computers. To specify the local computer, type the computer name, “localhost”, or a dot (.). When the computer is in a different domain than the user, the fully qualified domain name is required. You can also pipe a computer name (in quotes) to New-PSSession.

        To use an IP address in the value of the ComputerName parameter, the command must include the Credential parameter. Also, the computer must be configured for HTTPS transport or the IP address of the remote computer must be included in the WinRM TrustedHosts list on the local computer. For instructions for adding a computer name to the TrustedHosts list, see “How to Add a Computer to the Trusted Host List” in about_remote_TroubleShooting.

        Note: In Windows Vista and later versions of Windows, to include the local computer in the value of the ComputerName parameter, you must start Windows PowerShell with the “Run as administrator” option.

        Required?                    false
        Position?                    1
        Default value
        Accept pipeline input?     true (ByValue, ByPropertyName)
        Accept wildcard characters? false

    -ConfigurationName <string>
        Specifies the session configuration that is used for the new PSSession.

        Enter a configuration name or the fully qualified resource Uniform Resource Identifier (URI) for a session configuration. If you specify only the configuration name, the following schema URI is prepended: http://schemas.microsoft.com/powershell.

        The session configuration for a session is located on the remote computer. If the specified session configuration does not exist on the remote computer, the command fails.

        The default value is the value of the $PSSessionConfigurationName preference Variable on the local computer. If this preference Variable is not set, the default is Microsoft.PowerShell. For more information, see about_preference_variables.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -ConnectionURI <Uri[]>
        Specifies a Uniform Resource Identifier (URI) that defines the connection endpoint. The URI must be fully qualified.

        The format of this string is as follows:
            <Transport>://<ComputerName>:<Port>/<ApplicationName>

        The default value is as follows:
            http://localhost:80/WSMAN

        Valid values for the Transport segment of the URI are HTTP and HTTPS. If you do not specify a ConnectionURI, you can use the UseSSL, ComputerName, Port, and ApplicationName parameters to specify the URI values.

        If the destination computer redirects the connection to a different URI, Windows PowerShell prevents the redirection unless you use the AllowRedirection parameter in the command.

        Required?                    true
        Position?                    1
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -Credential <PSCredential>
        Specifies a user account that has permission to perform this action. The default is the current user.

        Type a user name, such as “User01”, “Domain01\User01”, or “User@Domain.com”, or enter a PSCredential object, such as one returned by the Get-Credential cmdlet.

        When you type a user name, you will be prompted for a password.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -Name <string[]>
        Specifies a friendly name for the PSSession.

        You can use the name to refer to the PSSession when using other cmdlets, such as Get-PSSession and Enter-PSSession. The name is not required to be unique to the computer or the current session.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Port <int>
        Specifies the network port on the remote computer that is used for this command. The default is port 80 (the HTTP port).

        Before using an alternate port, you must configure the WinRM listener on the remote computer to listen at that port. Use the following commands to configure the listener:

        1. winrm delete winrm/config/listener?Address=*+Transport=HTTP
        2. winrm create winrm/config/listener?Address=*+Transport=HTTP @{Port="<port-number>"}

        Do not use the Port parameter unless you must. The port setting in the command applies to all computers and sessions in which the command runs. An alternate port setting might prevent the command from running on all computers.

        Required?                    false
        Position?                    named
        Default value                80
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Session <PSSession[]>
        Uses the specified PSSession as a model for the new PSSession. This parameter creates new PSSessions with the same properties as the specified PSSessions.

        Enter a Variable that contains the PSSessions or a command that creates or gets the PSSessions, such as a New-PSSession or Get-PSSession command.

        The resulting PSSessions have the same computer name, application name, connection URI, port, configuration name, throttle limit, and Secure Sockets Layer (SSL) value as the originals, but they have a different display name, ID, and instance ID (GUID).

        Required?                    false
        Position?                    1
        Default value
        Accept pipeline input?     true (ByValue, ByPropertyName)
        Accept wildcard characters? false

    -SessionOption <PSSessionOption>
        Sets advanced options for the session. Enter a SessionOption object that you create by using the New-PSSessionOption cmdlet.

        The default values for the options are determined by the value of the $PSSessionOption preference Variable, if it is set. Otherwise, the session uses the system defaults.

        For a description of the session options, including the default values, see New-PSSessionOption. For information about the $PSSessionOption preference Variable, see about_preference_variables.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -ThrottleLimit <int>
        Specifies the maximum number of concurrent connections that can be established to run this command. If you omit this parameter or enter a value of 0 (zero), the default value, 32, is used.

        The throttle limit applies only to the current command, not to the session or to the computer.

        Required?                    false
        Position?                    named
        Default value                32
        Accept pipeline input?     false
        Accept wildcard characters? false

    -UseSSL [<SwitchParameter>]
        Uses the Secure Sockets Layer (SSL) protocol to establish a connection to the remote computer. By default, SSL is not used.

        WS-Management encrypts all Windows PowerShell content transmitted over the network. UseSSL is an additional protection that sends the data across an HTTPS connection instead of an HTTP connection.

        If you use this parameter, but SSL is not available on the port used for the command, the command fails.

        Required?                    false
        Position?                    named
        Default value
        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
    System.String, System.URI, System.Management.Automation.Runspaces.PSSession
        You can pipe a ComputerName (string), ConnectionURI (URI), or Session (PSSession) object to New-PSSession.

OUTPUTS
    System.Management.Automation.Runspaces.PSSession

NOTES

        This cmdlet uses the Windows PowerShell remoting infrastructure. To use this cmdlet, the local computer and any remote computers must be configured for Windows PowerShell remoting. For more information, see about_remote_requirements.

        In Windows Vista and later versions of Windows, to create a PSSession on the local computer, you must start Windows PowerShell with the “Run as administrator” option.

        When you are finished with the PSSession, use the Remove-PSSession cmdlet to delete the PSSession and release its resources.

    ————————– EXAMPLE 1 ————————–

    C:\PS>$s = New-PSSession

    Description
    ———–
    This command creates a new PSSession on the local computer and saves the PSSession in the $s Variable.

    You can now use this PSSession to run commands on the local computer.

    ————————– EXAMPLE 2 ————————–

    C:\PS>$Server01 = New-PSSession -ComputerName Server01

    Description
    ———–
    This command creates a new PSSession on the Server01 computer and saves it in the $Server01 Variable.

    When creating multiple PSSessions, assign them to Variables with useful names. This will help you manage the PSSessions in subsequent commands.

    ————————– EXAMPLE 3 ————————–

    C:\PS>$s1, $s2, $s3 = new-session -ComputerName server1,server2,server3

    Description
    ———–
    This command creates three new PSSessions, one on each of the computers specified by the ComputerName parameter.

    The command uses the assignment operator (=) to assign the new PSSessions to an array of Variables: $s1, $s2, $s3. It assigns the Server01 PSSession to $s1, the Server02 PSSession to $s2, and the Server03 PSSession to $s3.

    When you assign multiple objects to an array of Variables, Windows PowerShell assigns each object to a Variable in the array respectively. If there are more objects than Variables, all remaining objects are assigned to the last Variable. If there are more Variables than objects, the remaining Variables are empty (null).

    ————————– EXAMPLE 4 ————————–

    C:\PS>New-PSSession -ComputerName Server01 -Port 8081 -UseSSL -ConfigurationName E12

    Description
    ———–
    This command creates a new PSSession on the Server01 computer that connects to server port 8081 and uses the SSL protocol. The new PSSession uses an alternate session configuration called “E12”.

    Before setting the port, you must configure the WinRM listener on the remote computer to listen on port 8081. For more information, see the description of the Port parameter.

    ————————– EXAMPLE 5 ————————–

    C:\PS>New-PSSession -session $s -Credential domain01\user01

    Description
    ———–
    This command creates a new PSSession with the same properties as an existing PSSession. You can use this command format when the resources of an existing PSSession are exhausted and a new PSSession is needed to offload some of the demand.

    The command uses the Session parameter of New-PSSession to specify the PSSession saved in the $s Variable. It uses the credentials of the Domain1\Admin01 user to complete the command.

    ————————– EXAMPLE 6 ————————–

    C:\PS>$global:s = New-PSSession -ComputerName server1.domain44.corpnet.fabrikam.com -Credential domain01\admin01

    Description
    ———–
    This example shows how to create a PSSession with a global scope on a computer in a different domain.

    By default, PSSessions created at the command line are created with local scope and PSSessions created in a script have script scope.

    To create a PSSession with global scope, create a new PSSession and then store the PSSession in a Variable that is cast to a global scope. In this case, the $s Variable is cast to a global scope.

    The command uses the ComputerName parameter to specify the remote computer. Because the computer is in a different domain than the user account, the full name of the computer is specified along with the credentials of the user.

    ————————– EXAMPLE 7 ————————–

    C:\PS>$rs = Get-Content c:\test\servers.txt | New-PSSession -ThrottleLimit 50

    Description
    ———–
    This command creates a PSSession on each of the 200 computers listed in the Servers.txt file and it stores the resulting PSSession in the $rs Variable. The PSSessions have a throttle limit of 50.

    You can use this command format when the names of computers are stored in a database, spreadsheet, text file, or other text-convertible format.

    ————————– EXAMPLE 8 ————————–

    C:\PS>$s = New-PSSession -URI http://Server01:91/NewSession -Credential domain01\user01

    Description
    ———–
    This command creates a PSSession on the Server01 computer and stores it in the $s Variable. It uses the URI parameter to specify the transport protocol, the remote computer, the port, and an alternate session configuration. It also uses the Credential parameter to specify a user account with permission to create a session on the remote computer.

    ————————– EXAMPLE 9 ————————–

    C:\PS>$s = New-PSSession -ComputerName (Import-Csv servers.csv) -Credential domain01\admin01 -ThrottleLimit 16

    C:\PS> Invoke-Command -session $s -scriptblock {Get-Process powershell} -AsJob

    Description
    ———–
    These commands create a set of PSSessions and then run a background job in each of the PSSessions.

    The first command creates a new PSSession on each of the computers listed in the Servers.csv file. It uses the New-PSSession cmdlet to create the PSSession. The value of the ComputerName parameter is a command that uses the Import-Csv cmdlet to import the Servers.csv file and read its contents.

    The command uses the Credential parameter to create the PSSessions with the permission of a domain administrator, and it uses the ThrottleLimit parameter to limit the command to 16 concurrent connections. The command saves the PSSessions in the $s Variable.

    The second command uses the AsJob parameter of Invoke-Command to start a background job that runs a “Get-Process PowerShell” command in each of the PSSessions in $s.

    For more information about background jobs, see about_jobs and about_remote_Jobs.

    ————————– EXAMPLE 10 ————————–

    C:\PS>New-PSSession -ConnectionURI https://management.exchangelabs.com/Management

    Description
    ———–
    This command creates a new PSSession that connects to a computer that is specified by a URI instead of a computer name.

    ————————– EXAMPLE 11 ————————–

    C:\PS>$so = New-WSManSessionOption -SkipCACheck

    PS C:\> New-PSSession -ConnectionUri https://management.exchangelabs.com/Management -SessionOption $so -Credential server01\admin01

    Description
    ———–
    This example shows how to create and use a SessionOption parameter.

    The first command uses the New-WSManSessionOption cmdlet to create a session option. It saves the resulting SessionOption object in the $so parameter.

    The second command uses the option in a new session. The command uses the New-PSSession cmdlet to create a new session. The value of the SessionOption parameter is the SessionOption object in the $so Variable.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=135237
    about_pssessions
    about_remote
    Get-PSSession
    Remove-PSSession
    Enter-PSSession
    Exit-PSSession
    Invoke-Command

Import-PSSession

NAME
    Import-PSSession

SYNOPSIS
    Imports commands from another session into the current session.

SYNTAX
    Import-PSSession [-Session] <PSSession> [[-CommandName] <string[]>] [[-FormatTypeName] <string[]>] [-AllowClobber] [-ArgumentList <Object[]>] [-CommandType {Alias | Function | Filter | Cmdlet | ExternalScript | Application | Script | All}] [-Module <string[]>] [-Prefix <string>] [<CommonParameters>]

DESCRIPTION
    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 Get-Command 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.

PARAMETERS
    -AllowClobber [<SwitchParameter>]
        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.

        Required?                    false
        Position?                    named
        Default value                False
        Accept pipeline input?     false
        Accept wildcard characters? false

    -ArgumentList <Object[]>
        Imports the variant of the command that results from using the specified arguments (parameter values).

        For example, 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:”.

        Required?                    false
        Position?                    named
        Default value                All command in the PSSession, except for commands with the same names as commands in the current session.
        Accept pipeline input?     false
        Accept wildcard characters? false

    -CommandName <string[]>
        Imports only the 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.

        Required?                    false
        Position?                    3
        Default value                All commands in the PSSession, except for commands with the same names as commands in the current session.
        Accept pipeline input?     false
        Accept wildcard characters? true

    -CommandType <CommandTypes>
        Imports only the specified types of command objects. The default value is Cmdlet. Use “CommandType” or its Alias, “Type”.

        Valid values 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 blocks in the remote session.

        Required?                    false
        Position?                    named
        Default value                All command in the PSSession, except for commands with the same names as commands in the current session.
        Accept pipeline input?     false
        Accept wildcard characters? false

    -FormatTypeName <string[]>
        Imports 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 parameters, 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.

        Required?                    false
        Position?                    4
        Default value                Types in the System.Management.Automation namespace
        Accept pipeline input?     false
        Accept wildcard characters? true

    -Module <string[]>
        Imports 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 command in the PSSession, except for commands with the same names as commands in the current session.
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Prefix <string>
        Adds the specified 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 example, 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.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Session <PSSession>
        Specifies 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.

        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 this cmdlet.

OUTPUTS
    System.Management.Automation.PSModuleInfo
        Import-PSSession returns the same module object that New-Module and Get-Module return. However, the imported module is temporary and exists only in the current session. To create a permanent module on disk, use the Export-PSSession cmdlet.

NOTES

        Import-PSSession relies on the Windows PowerShell remoting infrastructure. To use this cmdlet, the computer must be configured for WS-Management remoting. For more information, see about_remote and about_remote_requirements.

        You cannot use Import-PSSession to import Variables or Windows PowerShell providers.

        When you import commands that have the same names as commands in the current session, the imported commands can hide Aliases, Functions, and cmdlets in the session and they can replace Functions and Variables in the session. For more information, see about_command_precedence.

        Import-PSSession converts all commands into Functions before it imports them. As a result, imported commands behave a bit differently than they would if they retained their original command type. For example, if you import a cmdlet from a PSSession and then import a cmdlet with the same name from a module or snap-in, the cmdlet that is imported from the PSSession always runs by default because Functions take precedence over cmdlets. Conversely, if you import an Alias into a session that has an Alias with the same name, the original Alias is always used, because Aliases take precedence over Functions. For more information, see about_command_precedence.

        Import-PSSession uses the Write-Progress cmdlet to display the progress of the command. You might see the progress bar while the command is running.

        To find the commands to import, Import-PSSession uses the Invoke-Command cmdlet to run a Get-Command command in the PSSession. To get formatting data for the commands, it uses the Get-FormatData cmdlet. You might see error messages from Invoke-Command, Get-Command, and Get-FormatData when you run an Import-PSSession command. Also, Import-PSSession cannot import commands from a PSSession that does not include the Get-Command, Get-FormatData, Select-Object, and Get-Help cmdlets.

        Imported 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 Import-PSSession. To import commands from a profile, use an Invoke-Command command to run the profile in the PSSession manually before importing commands.

        The temporary module that Import-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.

        To use Import-PSSession, the execution policy in the current session cannot be Restricted or AllSigned, because the module that Import-PSSession creates contains unsigned script files that are prohibited by these policies. To use Import-PSSession without changing the execution policy for the local computer, use the Scope parameter of Set-ExecutionPolicy to set a less restrictive execution policy for a single process.

    ————————– EXAMPLE 1 ————————–

    C:\PS>$s = New-PSSession -computername Server01

    C:\PS> Import-PSSession -Session $s

    Description
    ———–
    This command imports all commands from a PSSession on the Server01 computer into the current session, except for commands that have the same names as commands in the current session.

    Because this command does not use the CommandName parameter, it also imports all of the formatting data required for the imported commands.

    ————————– EXAMPLE 2 ————————–

    C:\PS>$s = New-PSSession https://ps.testlabs.com/powershell

    C:\PS> Import-PSSession -Session $s -CommandName *-test -FormatTypeName *

    C:\PS> new-test -name test1

    C:\PS> get-test test1 | run-test

    Description
    ———–
    These commands import the commands with names that end in “-test” from a PSSession into the local session, and then they show how to use an imported cmdlet.

    The first command uses the New-PSSession cmdlet to create a PSSession. It saves the PSSession in the $s Variable.

    The second command uses the Import-PSSession cmdlet to import commands from the PSSession in $s into the current session. It uses the CommandName parameter to specify commands with the Test noun and the FormatTypeName parameter to import the formatting data for the Test commands.

    The third and fourth commands use the imported commands in the current session. Because imported commands are actually added to the current session, you use the local syntax to run them. You do not need to use the Invoke-Command cmdlet to run an imported command.

    ————————– EXAMPLE 3 ————————–

    C:\PS>$s1 = New-PSSession -computername s1

    C:\PS> $s2 = New-PSSession -computername s2

    C:\PS> Import-PSSession -Session s1 -type cmdlet -name New-Test, Get-Test -FormatTypeName *

    C:\PS> Import-PSSession -Session s2 -type cmdlet -name Set-Test -FormatTypeName *

    C:\PS> new-test Test1 | set-test -runtype full

    Description
    ———–
    This example shows that you can use imported cmdlets just as you would use local cmdlets.

    These commands import the New-Test and Get-Test cmdlets from a PSSession on the Server01 computer and the Set-Test cmdlet from a PSSession on the Server02 computer.

    Even though the cmdlets were imported from different PSSessions, you can pipe an object from one cmdlet to another without error.

    ————————– EXAMPLE 4 ————————–

    C:\PS>$s = New-PSSession -computername Server01

    C:\PS> Import-PSSession -Session $s -CommandName *-test* -FormatTypeName *

    C:\PS> $batch = new-test -name Batch -asjob

    C:\PS> Receive-Job $batch

    Description
    ———–
    This example shows how to run an imported command as a background job.

    Because imported commands might take longer to run than local commands, Import-PSSession adds an AsJob parameter to every imported command. The AsJob parameter lets you run the command as a background job.

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

    The second command uses Import-PSSession to import the Test cmdlets from the PSSession in $s into the current session.

    The third command uses the AsJob parameter of the imported New-Test cmdlet to run a New-Test command as a background job. The command saves the job object that New-Test returns in the $batch Variable.

    The fourth command uses the Receive-Job cmdlet to get the results of the job in the $batch Variable.

    ————————– EXAMPLE 5 ————————–

    C:\PS>$s = New-PSSession -comp Server01

    C:\PS> Invoke-Command -Session $s {Import-Module TestManagement}

    C:\PS> Import-PSSession -Session $s -Module TestManagement

    Description
    ———–
    This example shows how to import the cmdlets and Functions from a Windows PowerShell module on a remote computer into the current session.

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

    The second command uses the Invoke-Command cmdlet to run an Import-Module command in the PSSession in $s.

    Typically, the module would be added to all sessions by an Import-Module command in a Windows PowerShell profile, but profiles are not run in PSSessions.

    The third command uses the Module parameter of Import-PSSession to import the cmdlets and Functions in the module into the current session.

    ————————– EXAMPLE 6 ————————–

    C:\PS>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_tcw1zunz.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_tcw1zunz.ttf
    ModuleType        : Script
    PrivateData     : {ImplicitRemoting}
    AccessMode        : ReadWrite
    ExportedAliases : {}
    ExportedCmdlets : {}
    ExportedFunctions : {[Get-Date, Get-Date], [SearchHelp, SearchHelp]}
    ExportedVariables : {}
    NestedModules     : {}

    Description
    ———–
    This example shows that Import-PSSession creates a module in a temporary file on disk. It also shows that all commands are converted into Functions before they are imported into the current session.

    The command uses the Import-PSSession cmdlet to import a Get-Date cmdlet and a SearchHelp Function into the current session.

    The Import-PSSession cmdlet returns a PSModuleInfo object that represents the temporary module. The value of the Path property shows that Import-PSSession created a script module (.psm1) file in a temporary location. The ExportedFunctions property shows that the Get-Date cmdlet and the SearchHelp Function were both imported as Functions.

    ————————– EXAMPLE 7 ————————–

    C:\PS>Import-PSSession $s -CommandName Get-Date -FormatTypeName * -AllowClobber

    C:\PS> Get-Command Get-Date

    CommandType Name     Definition
    ———– —-     ———-
    Function     Get-Date
    Cmdlet        Get-Date Get-Date [[-Date] <DateTime>] [-Year <Int32>] [-Month <Int32>]

    C:\PS> Get-Date
    09074

    C:\PS> (Get-Command -type cmdlet -name Get-Date).pssnapin.name
    Microsoft.PowerShell.Utility

    C:\PS> Microsoft.PowerShell.Utility\Get-Date

    Sunday, March 15, 2009 2:08:26 PM

    Description
    ———–
    This example shows how to run a command that is hidden by an imported command.

    The first command imports a Get-Date cmdlet from the PSSession in the $s Variable. Because the current session includes a Get-Date cmdlet, the AllowClobber parameter is required in the command.

    The second command uses the Get-Command cmdlet to get the Get-Date commands in the current session. The output shows that the session includes the original Get-Date cmdlet and a Get-Date Function. The Get-Date Function runs the imported Get-Date cmdlet in the PSSession in $s.

    The third command runs a Get-Date command. Because Functions take precedence over cmdlets, Windows PowerShell runs the imported Get-Date Function, which returns a Julian date.

    The fourth and fifth commands show how to use a qualified name to run a command that is hidden by an imported command.

    The fourth command gets the name of the Windows PowerShell snap-in that added the original Get-Date cmdlet to the current session.

    The fifth command uses the snap-in-qualified name of the Get-Date cmdlet to run a Get-Date command.

    For more information about command precedence and hidden commands, see about_command_precedence.

    ————————– EXAMPLE 8 ————————–

    C:\PS>Import-PSSession -Session $s -CommandName *Item* -AllowClobber

    Description
    ———–
    This command imports commands whose names include “Item” from the PSSession in $s. Because the command includes the CommandName parameter but not the FormatTypeData parameter, only the command is imported.

    Use this command when you are using Import-PSSession to run a command on a remote computer and you already have the formatting data for the command in the current session.

    ————————– EXAMPLE 9 ————————–

    C:\PS>$m = Import-PSSession -Session $s -CommandName *bits* -FormatTypeName *bits*

    C:\PS> 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-BitsTransfer

    Description
    ———–
    This command shows how to use the Module parameter of Get-Command to find out which commands were imported into the session by an Import-PSSession command.

    The first command uses the Import-PSSession cmdlet to import commands whose names include “bits” from the PSSession in the $s Variable. The Import-PSSession command returns a temporary module, and the command saves the module in the $m Variable.

    The second command uses the Get-Command cmdlet to get the commands that are exported by the module in the $m Variable.

    The Module parameter takes a string value, which is designed for the module name. However, when you submit a module object, Windows PowerShell uses the ToString method on the module object, which returns the module name.

    The Get-Command command is the equivalent of “Get-Command $m.name”.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=135221
    about_command_precedence
    New-PSSession
    Export-PSSession
    about_jobs
    about_pssessions

Get-PSSession

NAME
    Get-PSSession

SYNOPSIS
    Gets the Windows PowerShell sessions (PSSessions) in the current session.

SYNTAX
    Get-PSSession [[-ComputerName] <string[]>] [<CommonParameters>]

    Get-PSSession [-Id] <Int32[]> [<CommonParameters>]

    Get-PSSession [-InstanceId <Guid[]>] [<CommonParameters>]

    Get-PSSession [-Name <string[]>] [<CommonParameters>]

DESCRIPTION
    The Get-PSSession cmdlet gets the Windows PowerShell sessions (PSSessions) that were created in the current session.

    Without parameters, Get-PSSession gets all of the PSSessions created in the current session. You can use the parameters of Get-PSSession to get the sessions that are connected to particular computers, or you can identify sessions by their names, IDs, or instance IDs.

    For more information about Windows PowerShell sessions, see about_pssessions.

PARAMETERS
    -ComputerName <string[]>
        Gets only the PSSessions that are connected to the specified computers. Wildcards are permitted.

        Type the NetBIOS name, an IP address, or a fully-qualified domain name of one or more computers. To specify the local computer, type the computer name, “localhost”, or a dot (.).

        Required?                    false
        Position?                    1
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? true

    -Id <Int32[]>
        Gets only the PSSessions with the specified IDs. Type one or more IDs (separated by commas), or use the range operator (..) to specify a range of IDs.

        An ID is an integer that uniquely identifies the PSSession in the current session. It is easier to remember and type than the InstanceId, but it is unique only within the current session. To find the ID of a PSSession, use Get-PSSession without parameters.

        Required?                    true
        Position?                    1
        Default value                All sessions in the shell
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -InstanceId <Guid[]>
        Gets only the PSSessions with the specified instance IDs.

        The instance ID is a GUID that uniquely identifies a PSSession on a local or remote computer. The InstanceID is unique, even when you have multiple sessions running in Windows PowerShell.

        The InstanceID is stored in the InstanceID property of the object that represents a PSSession. To find the InstanceID of the PSSessions in the current session, type “Get-PSSession | Format-Table Name, ComputerName, InstanceId”.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? true

    -Name <string[]>
        Gets only the PSSessions with the specified friendly names. Wildcards are permitted.

        To find the names of the PSSessions in the current session, type “Get-PSSession” without parameters.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? true

    <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 input to this cmdlet.

OUTPUTS
    System.Management.Automation.Runspaces.PSSession
        Get-PSSession returns a PSSession object for each PSSession that it gets.

NOTES

        Get-PSSession gets the PSSessions that were created in the current session. It does not get the session that is created when you open Windows PowerShell, and it does not get PSSessions that were created in other sessions or on other computers, even if they connect to the local computer.

    ————————– EXAMPLE 1 ————————–

    C:\PS>Get-PSSession

    Description
    ———–
    This command gets all of the PSSessions that were created in the current session.

    It does not get PSSessions that were created in other sessions or on other computers, even if they connect to this computer.

    ————————– EXAMPLE 2 ————————–

    C:\PS>$s = Get-PSSession -ComputerName Server02

    Description
    ———–
    This command gets the PSSessions that are connected to the Server02 computer and saves them in the $p Variable.

    ————————– EXAMPLE 3 ————————–

    C:\PS>New-PSSession -ComputerName Server01, Server02, Server03

    C:\PS> $s1, $s2, $s3 = Get-PSSession

    Description
    ———–
    This example shows how to save the results of a Get-PSSession command in multiple Variables.

    The first command uses the New-PSSession cmdlet to create PSSessions on three remote computers.

    The second command uses a Get-PSSession cmdlet to get the three PSSessions. It then saves each of the PSSessions in a separate Variable.

    When Windows PowerShell assigns an array of objects to an array of Variables, it assigns the first object to the first Variable, the second object to the second Variable, and so on. If there are more objects than Variables, it assigns all remaining objects to the last Variable in the array. If there are more Variables than objects, the extra Variables are not used.

    ————————– EXAMPLE 4 ————————–

    C:\PS>Get-PSSession | Format-Table -property computername, InstanceID

    C:\PS> $s = Get-PSSession -InstanceID a786be29-a6bb-40da-80fb-782c67f7db0f

    C:\PS> Remove-PSSession -session $s

    Description
    ———–
    This example shows how to get a PSSession by using its instance ID, and then to delete the PSSession.

    The first command gets all of the PSSessions on the local computer. It sends the PSSessions to the Format-Table cmdlet, which displays the ComputerName and InstanceID properties of each PSSession.

    The second command uses the Get-PSSession cmdlet to get a particular PSSession and to save it in the $s Variable. The command uses the InstanceID parameter to identify the PSSession.

    The third command uses the Remove-PSSession cmdlet to delete the PSSession in the $s Variable.

    ————————– EXAMPLE 5 ————————–

    C:\PS>Get-PSSession -ComputerName Serv*

    Description
    ———–
    This command gets all the PSSessions that connect to computers that have computer names that begin with “Serv”.

    ————————– EXAMPLE 6 ————————–

    C:\PS>Get-PSSession -name Test*, Ux*

    Description
    ———–
    This command gets PSSessions that have names that begin with “Test” or “Ux”.

    ————————– EXAMPLE 7 ————————–

    C:\PS>Get-PSSession 2

    Description
    ———–
    This command gets the PSSession with ID 2.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=135219
    about_pssessions
    about_remote
    New-PSSession
    Remove-PSSession
    Enter-PSSession
    Exit-PSSession
    Invoke-Command

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.

    ————————– 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

Enter-PSSession

NAME
    Enter-PSSession

SYNOPSIS
    Starts an interactive session with a remote computer.

SYNTAX
    Enter-PSSession [-ComputerName] <string> [-ApplicationName <string>] [-Authentication {Default | Basic | Negotiate | NegotiateWithImplicitCredential | Credssp | Digest | Kerberos}] [-CertificateThumbprint <string>] [-ConfigurationName <string>] [-Credential <PSCredential>] [-Port <int>] [-SessionOption <PSSessionOption>] [-UseSSL] [<CommonParameters>]

    Enter-PSSession [[-Id] <int>] [<CommonParameters>]

    Enter-PSSession [-InstanceId <Guid>] [<CommonParameters>]

    Enter-PSSession [-Name <string>] [<CommonParameters>]

    Enter-PSSession [[-Session] <PSSession>] [<CommonParameters>]

    Enter-PSSession [[-ConnectionURI] <Uri>] [-AllowRedirection] [-Authentication {Default | Basic | Negotiate | NegotiateWithImplicitCredential | Credssp | Digest | Kerberos}] [-CertificateThumbprint <string>] [-ConfigurationName <string>] [-Credential <PSCredential>] [-SessionOption <PSSessionOption>] [<CommonParameters>]

DESCRIPTION
    The Enter-PSSession cmdlet starts an interactive session with a single remote computer. During the session, the commands that you type run on the remote computer, just as though you were typing directly on the remote computer. You can have only one interactive session at a time.

    Typically, you use the ComputerName parameter to specify the name of the remote computer. However, you can also use a session that you create by using New-PSSession for the interactive session.

    To end the interactive session and disconnect from the remote computer, use the Exit-PSSession cmdlet, or type “exit”.

PARAMETERS
    -AllowRedirection [<SwitchParameter>]
        Allows redirection of this connection to an alternate Uniform Resource Identifier (URI).

        When you use the ConnectionURI parameter, the remote destination can return an instruction to redirect to a different URI. By default, Windows PowerShell does not redirect connections, but you can use this parameter to allow it to redirect the connection.

        Allows redirection of this connection to an alternate URI.

        When you use the ConnectionURI parameter, the remote destination can return an instruction to redirect to a different URI. By default, Windows PowerShell does not redirect connections, but you can use the AllowRedirection parameter to allow it to redirect the connection.

        You can also limit the number of times that the connection is redirected by setting the MaximumConnectionRedirectionCount property of the $PSSessionOption preference Variable, or the MaximumConnectionRedirectionCount property of the value of the SessionOption parameter. The default value is 5. For more information, see the description of the SessionOption parameter, and see New-PSSessionOption.

        Required?                    false
        Position?                    named
        Default value                False
        Accept pipeline input?     false
        Accept wildcard characters? false

    -ApplicationName <string>
        Specifies the application name segment of the connection URI. Use this parameter to specify the application name when you are not using the ConnectionURI parameter in the command.

        The default value is the value of the $PSSessionApplicationName preference Variable on the local computer. If this preference Variable is not defined, the default value is WSMan. This value is appropriate for most uses. For more information, see about_preference_variables.

        The WinRM service uses the application name to select a listener to service the connection request. The value of this parameter should match the value of the URLPrefix property of a listener on the remote computer.

        Required?                    false
        Position?                    named
        Default value                WSMan
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -Authentication <AuthenticationMechanism>
        Specifies the mechanism that is used to authenticate the user’s credentials. Valid values are “Default”, “Basic”, “Credssp”, “Digest”, “Kerberos”, “Negotiate”, and “NegotiateWithImplicitCredential”. The default value is “Default”.

        CredSSP authentication is available only in Windows Vista, Windows Server 2008, and later versions of Windows.

        For information about the values of this parameter, see the description of the System.Management.Automation.Runspaces.AuthenticationMechanism enumeration in the MSDN (Microsoft Developer Network) library at http://go.microsoft.com/fwlink/?LinkId=144382.

        Caution: Credential Security Service Provider (CredSSP) authentication, in which the user’s credentials are passed to a remote computer to be authenticated, is designed for commands that require authentication on more than one resource, such as accessing a remote network share. This mechanism increases the security risk of the remote operation. If the remote computer is compromised, the credentials that are passed to it can be used to control the network session.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -CertificateThumbprint <string>
        Specifies the digital public key Certificate (X509) of a user account that has permission to perform this action. Enter the Certificate thumbprint of the Certificate.

        Certificates are used in client Certificate-based authentication. They can be mapped only to local user accounts; they do not work with domain accounts.

        To get a Certificate thumbprint, use the Get-Item or Get-ChildItem command in the Windows PowerShell Cert: drive.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -ComputerName <string>
        Starts an interactive session with the specified remote computer. Enter only one computer name. The default is the local computer.

        Type the NetBIOS name, an IP address, or a fully qualified domain name of one or more remote computers. You can also pipe a computer name to Enter-PSSession.

        To use an IP address in the value of the ComputerName parameter, the command must include the Credential parameter. Also, the computer must be configured for HTTPS transport or the IP address of the remote computer must be included in the WinRM TrustedHosts list on the local computer. For instructions for adding a computer name to the TrustedHosts list, see “How to Add a Computer to the Trusted Host List” in about_remote_TroubleShooting.

        Note: In Windows Vista and later versions of Windows, to include the local computer in the value of the ComputerName parameter, you must start Windows PowerShell with the “Run as administrator” option.

        Required?                    true
        Position?                    1
        Default value
        Accept pipeline input?     true (ByValue, ByPropertyName)
        Accept wildcard characters? false

    -ConfigurationName <string>
        Specifies the session configuration that is used for the interactive session.

        Enter a configuration name or the fully qualified resource URI for a session configuration. If you specify only the configuration name, the following schema URI is prepended: http://schemas.microsoft.com/powershell.

        The session configuration for a session is located on the remote computer. If the specified session configuration does not exist on the remote computer, the command fails.

        The default value is the value of the $PSSessionConfigurationName preference Variable on the local computer. If this preference Variable is not set, the default is Microsoft.PowerShell. For more information, see about_preference_variables.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -ConnectionURI <Uri>
        Specifies a Uniform Resource Identifier (URI) that defines the connection endpoint for the interactive session. The URI must be fully qualified.

        The format of this string is as follows:
            <Transport>://<ComputerName>:<Port>/<ApplicationName>

        The default value is as follows:
            http://localhost:80/WSMAN

        Valid values for the Transport segment of the URI are HTTP and HTTPS. If you do not specify a ConnectionURI, you can use the UseSSL, ComputerName, Port, and ApplicationName parameters to specify the URI values.

        If the destination computer redirects the connection to a different URI, Windows PowerShell prevents the redirection unless you use the AllowRedirection parameter in the command.

        Required?                    false
        Position?                    2
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -Credential <PSCredential>
        Specifies a user account that has permission to perform this action. The default is the current user.

        Type a user name, such as “User01”, “Domain01\User01”, or “User@Domain.com”, or enter a PSCredential object, such as one returned by the Get-Credential cmdlet.

        When you type a user name, you will be prompted for a password.

        Required?                    false
        Position?                    named
        Default value                Current user
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -Id <int>
        Specifies the ID of an existing session. Enter-PSSession uses the specified session for the interactive session.

        To find the ID of a session, use the Get-PSSession cmdlet.

        Required?                    false
        Position?                    1
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -InstanceId <Guid>
        Specifies the instance ID of an existing session. Enter-PSSession uses the specified session for the interactive session.

        The instance ID is a GUID. To find the instance ID of a session, use the Get-PSSession cmdlet. You can also use the Session, Name, or ID parameters to specify an existing session. Or, you can use the ComputerName parameter to start a temporary session.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -Name <string>
        Specifies the friendly name of an existing session. Enter-PSSession uses the specified session for the interactive session.

        If the name that you specify matches more than one session, the command fails. You can also use the Session, InstanceID, or ID parameters to specify an existing session. Or, you can use the ComputerName parameter to start a temporary session.

        To establish a friendly name for a session, use the Name parameter of the New-PSSession cmdlet.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -Port <int>
        Specifies the network port on the remote computer used for this command. The default is port 80 (the HTTP port).

        Before using an alternate port, you must configure the WinRM listener on the remote computer to listen at that port. Use the following commands to configure the listener:

        1. winrm delete winrm/config/listener?Address=*+Transport=HTTP
        2. winrm create winrm/config/listener?Address=*+Transport=HTTP @{Port=”<port-number>”}

        Do not use the Port parameter unless you must. The port setting in the command applies to all computers or sessions on which the command runs. An alternate port setting might prevent the command from running on all computers.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Session <PSSession>
        Specifies a Windows PowerShell session (PSSession) to use for the interactive session. This parameter takes a session object. You can also use the Name, InstanceID, or ID parameters to specify a PSSession.

        Enter a Variable that contains a session object or a command that creates or gets a session object, such as a New-PSSession or Get-PSSession command. You can also pipe a session object to Enter-PSSession. You can submit only one PSSession with this parameter. If you enter a Variable that contains more than one PSSession, the command fails.

        When you use Exit-PSSession or the EXIT keyword, the interactive session ends, but the PSSession that you created remains open and available for use.

        Required?                    false
        Position?                    1
        Default value
        Accept pipeline input?     true (ByValue, ByPropertyName)
        Accept wildcard characters? true

    -SessionOption <PSSessionOption>
        Sets advanced options for the session. Enter a SessionOption object that you create by using the New-PSSessionOption cmdlet.

        The default values for the options are determined by the value of the $PSSessionOption preference Variable, if it is set. Otherwise, the session uses the system defaults.

        For a description of the session options, including the default values, see New-PSSessionOption. For information about the $PSSessionOption preference Variable, see about_preference_variables.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -UseSSL [<SwitchParameter>]
        Uses the Secure Sockets Layer (SSL) protocol to establish a connection to the remote computer. By default, SSL is not used.

        WS-Management encrypts all Windows PowerShell content transmitted over the network. UseSSL is an additional protection that sends the data across an HTTPS connection instead of an HTTP connection.

        If you use this parameter, but SSL is not available on the port used for the command, the command fails.

        Required?                    false
        Position?                    named
        Default value
        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
    System.String or System.Management.Automation.Runspaces.PSSession
        You can pipe a computer name (a string) or a session object to Enter-PSSession.

OUTPUTS
    None
        The cmdlet does not return any output.

NOTES

        — To connect to a remote computer, you must be a member of the Administrators group on the remote computer.

        — In Windows Vista and later versions of Windows, to start an interactive session on the local computer, you must start Windows PowerShell with the “Run as administrator” option.

        — When you use Enter-PSSession, your user profile on the remote computer is used for the interactive session. The commands in the remote user profile, including commands to add Windows PowerShell snap-ins and to change the command prompt, run before the remote prompt is displayed.

        — Enter-PSSession uses the UI culture setting on the local computer for the interactive session. To find the local UI culture, use the $UICulture automatic Variable.

        — Enter-PSSession requires the Get-Command, Out-Default, and Exit-PSSession cmdlets. If these cmdlets are not included in the session configuration on the remote computer, the Enter-PSSession commands fails.

        — Unlike Invoke-Command, which parses and interprets the commands before sending them to the remote computer, Enter-PSSession sends the commands directly to the remote computer without interpretation.

    ————————– EXAMPLE 1 ————————–

    C:\PS>Enter-PSSession

    LocalHost\PS>

    Description
    ———–
    This command starts an interactive session on the local computer. The command prompt changes to indicate that you are now running commands in a different session.

    The commands that you enter run in the new session, and the results are returned to the default session as text.

    ————————– EXAMPLE 2 ————————–

    C:\PS>Enter-PSSession -computer Server01

    Server01\PS> Get-Process powershell > C:\ps-test\process.txt

    Server01\PS> Exit-PSSession
    C:\PS>

    C:\PS> dir C:\ps-test\process.txt
    Get-ChildItem : Cannot find path ‘C:\ps-test\process.txt’ because it does not exist.
    At line:1 char:4
    + dir <<<< c:\ps-test\process.txt

    Description
    ———–
    This command shows how to work in an interactive session with a remote computer.

    The first command uses the Enter-PSSession cmdlet to start an interactive session with Server01, a remote computer. When the session starts, the command prompt changes to include the computer name.

    The second command gets the PowerShell process and redirects the output to the Process.txt file. The command is submitted to the remote computer, and the file is saved on the remote computer.

    The third command uses the Exit keyword to end the interactive session and close the connection.

    The fourth command confirms that the Process.txt file is on the remote computer. A Get-ChildItem (“dir”) command on the local computer cannot find the file.

    ————————– EXAMPLE 3 ————————–

    C:\PS>$s = New-PSSession -ComputerName Server01

    C:\PS> Enter-PSSession -session $s

    Server01\PS>

    Description
    ———–
    These commands use the Session parameter of Enter-PSSession to run the interactive session in an existing Windows PowerShell session (PSSession).

    ————————– EXAMPLE 4 ————————–

    C:\PS>Enter-PSSession -ComputerName Server01 -Port 90 -Credential domain01\user01

    Server01\PS>

    Description
    ———–
    This command starts an interactive session with the Server01 computer. It uses the Port parameter to specify the port and the Credential parameter to specify the account of a user with permission to connect to the remote computer.

    ————————– EXAMPLE 5 ————————–

    C:\PS>Enter-PSSession -ComputerName Server01

    Server01:\PS> Exit-PSSession

    C:\PS>

    Description
    ———–
    This example shows how to start and stop an interactive session. The first command uses the Enter-PSSession cmdlet to start an interactive session with the Server01 computer.

    The second command uses the Exit-PSSession cmdlet to end the session. You can also use the Exit keyword to end the interactive session. Exit-PSSession and Exit have the same effect.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=135210
    about_pssessions
    about_remote
    New-PSSession
    Get-PSSession
    Exit-PSSession
    Remove-PSSession
    Invoke-Command

Exit-PSSession

NAME
    Exit-PSSession

SYNOPSIS
    Ends an interactive session with a remote computer.

SYNTAX
    Exit-PSSession [<CommonParameters>]

DESCRIPTION
    The Exit-PSSession cmdlet ends interactive sessions that you started by using Enter-PSSession.

    You can also use the Exit keyword to end an interactive session. The effect is the same as using Exit-PSSession.

PARAMETERS
    <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 Exit-PSSession.

OUTPUTS
    None
        This cmdlet does not return any output.

NOTES

        This cmdlet takes only the common parameters.

    ————————– EXAMPLE 1 ————————–

    C:\PS>Enter-PSSession -computername Server01

    Server01\PS> Exit-PSSession

    C:\PS>

    Description
    ———–
    These commands start and then stop an interactive session with the Server01 remote computer.

    ————————– EXAMPLE 2 ————————–

    C:\PS>$s = New-PSSession -computername Server01

    C:\PS> Enter-PSSession -session $s

    Server01\PS> Exit-PSSession

    C:\PS> $s

    Id Name            ComputerName    State    ConfigurationName
    — —-            ————    —–    —————–
    1 Session1        Server01        Opened Microsoft.PowerShell

    Description
    ———–
    These commands start and stop an interactive session with the Server01 computer that uses a Windows PowerShell session (PSSession).

    Because the interactive session was started by using a Windows PowerShell session (PSSession), the PSSession is still available when the interactive session ends. If you use the ComputerName parameter, Enter-PSSession creates a temporary session that it closes when the interactive session ends.

    The first command uses the New-PSSession cmdlet to create a PSSession on the Server01 computer. The command saves the PSSession in the $s Variable.

    The second command uses the Enter-PSSession cmdlet to start an interactive session using the PSSession in $s.

    The third command uses the Exit-PSSession cmdlet to stop the interactive session.

    The final command displays the PSSession in the $s Variable. The State property shows the PSSession is still open and available for use.

    ————————– EXAMPLE 3 ————————–

    C:\PS>Enter-PSSession -computername Server01

    Server01\PS> exit

    C:\PS>

    Description
    ———–
    This command uses the Exit keyword to stop an interactive session started by using the Enter-PSSession cmdlet. The Exit keyword has the same effect as using Exit-PSSession.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=135212
    about_pssessions
    about_remote
    Enter-PSSession
    New-PSSession
    Get-PSSession
    Remove-PSSession
    Invoke-Command