PowerShell New-PSSession Cmdlet
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed Remove-EventLog cmdlet in PowerShell.
https://cloudaffaire.com/powershell-remove-eventlog-cmdlet/
In this blog post, we will discuss New-PSSession cmdlet in PowerShell. You can use New-PSSession cmdlet to create a PowerShell session on a local or remote computer. Using New-PSSession cmdlet, you can establish a persistent connection to a local or remote computer using which you can execute scripts, run commands, share data, etc. You can also use “ComputerName” parameter with a supported cmdlet to execute the cmdlet on a remote computer without creating a new PSSession. New-PSSession cmdlet uses PowerShell remoting infrastructure. To use this cmdlet, the local computer and any remote computers must be configured for PowerShell remoting. Below are the prerequisites for using New-PSSession cmdlet.
- Windows PowerShell 2.0 or later
- The Microsoft .NET Framework 2.0 or later
- Windows Remote Management 2.0
- The current user must be a member of the Administrators group on the remote computer or provide the credentials of an administrator.
To configure PowerShell to receive remote commands:
- Start PowerShell with the “Run as administrator” option.
- At the command prompt, type: Enable-PSRemoting
New-PSSession Cmdlet Syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
## New-PSSession ## [[-ComputerName] ## [-Credential ## [[-Session] ## [-Name ## [-EnableNetworkAccess] ## [-ConfigurationName ## [-Port ## [-UseSSL] ## [-ApplicationName ## -ContainerId ## [-RunAsAdministrator] ## [-ThrottleLimit ## [-ConfigurationName ## [-VMId] ## -VMName ## [-ConnectionUri] ## [-AllowRedirection] ## [-SessionOption ## [-Authentication ## [-CertificateThumbprint ## [ |
New-PSSession Cmdlet Argument List:
- –AllowRedirection: Indicates that this cmdlet allows redirection of this connection to an alternate Uniform Resource Identifier (URI).
- –ApplicationName: 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.
- –Authentication: Specifies the mechanism that is used to authenticate the user’s credentials. The acceptable values for this parameter are:
- Default
- Basic
- Credssp
- Digest
- Kerberos
- Negotiate
- NegotiateWithImplicitCredential
- –CertificateThumbprint: 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.
- –ComputerName: Specifies an array of names of computers. This cmdlet creates a persistent connection (PSSession) to the specified computer. If you enter multiple computer names, New-PSSession creates multiple PSSession objects, one for each computer. The default is the local computer.
- –ConfigurationName: Specifies the session configuration that is used for the new PSSession.
- –ConnectionUri: Specifies a URI that defines the connection endpoint for the 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:5985/WSMAN
- –ContainerId: Specifies an array of IDs of containers. This cmdlet starts an interactive session with each of the specified containers.
- –Credential: Specifies a user account that has permission to do this action. The default is the current user.
- –EnableNetworkAccess: Indicates that this cmdlet adds an interactive security token to loopback sessions. The interactive token lets you run commands in the loopback session that get data from other computers.
- –Name: Specifies a friendly name for the PSSession.
- –Port: Specifies the network port on the remote computer that is used for this connection. To connect to a remote computer, the remote computer must be listening on the port that the connection uses. The default ports are 5985, which is the WinRM port for HTTP, and 5986, which is the WinRM port for HTTPS.
- –RunAsAdministrator: Indicates that the PSSession runs as administrator.
- –Session: Specifies an array of PSSession objects that this cmdlet uses as a model for the new PSSession.
- –SessionOption: Specifies advanced options for the session. Enter a SessionOption object, such as one that you create by using the New-PSSessionOption cmdlet, or a hash table in which the keys are session option names and the values are session option values.
- –ThrottleLimit: 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.
- –UseSSL: Indicates that this cmdlet uses the SSL protocol to establish a connection to the remote computer. By default, SSL is not used.
- –VMId: Specifies an array of ID of virtual machines. This cmdlet starts an interactive session with each of the specified virtual machines.
- –VMName: Specifies an array of names of virtual machines. This cmdlet starts an interactive session with each of the specified virtual machines.
PowerShell New-PSSession Cmdlet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
######################################### ## PowerShell | Cmdlet | New-PSSession ## ######################################### ## PowerShell Latest Version (5) ## Run PowerShell as admin ## create a new PSSession with a remote computer New-PSSession -ComputerName "System1" -Name "MyPsSession" ## get PSSession details Get-PSSession ## start interactive PSSession with a remote computer Enter-PSSession -Name "MyPsSession" ## execute any command on a remote computer hostname ## end interactive PSSession with a remote computer Exit-PSSession ## disconnect PSSession with a remote computer Disconnect-PSSession -Name "MyPsSession" Get-PSSession ## connect to a disconnected PSSession with a remote computer Connect-PSSession -Name "MyPsSession" ## close PSSession with a remote computer Remove-PSSession -Name "MyPsSession" |
Hope you have enjoyed this article. In the next blog post, we will discuss Get-PSSession CmdLet in PowerShell.
To get more details on PowerShell, kindly follow below official documentation
For simple configuration on a single remote machine, entering a remote Windows PowerShell session is the answer. To enter a remote Windows PowerShell session, use the