Hello Folks, this is just a simple post on how to open and enter a Remote PowerShell Session from one server to another. A lot of tasks on different servers can be accomplished in a convenient way from one server.
For instance if you want to Change the Keyboard Layout on the login screen of a new Server Core installation.
We will use the cmdlets below to open, enter, exit, and remove a Remote PowerShell session.
Remote PowerShell Prerequisites:
If you want to go for sure firstly check the PS-Remoting state of the remote computer with:
Afterward, verify the state of the WinRM service on the remote computer that it is running:
Remote PowerShell Session Cmdlets:
New-PSSession – [Create a new persistent connection to the target host]
Enter-PSSession – [Enter a session as interactive session]
Exit-PSSession – [Exit session. The session will still be alive in the background]
Disconnect-PSSession – [Disconnect from the session]
Connect-PSSession – [connect to a disconnected session]
Remove-PSSession – [Remove an earlier created persistent session]
Ok, here we go …
Create a Remote PowerShell Session with New-PSSession:
New-PSSession -Name NameOfSession -ComputerName NameOfRemoteComputer -Credential Domain\Username.
In case you don’t want to give the remote PowerShell session a name it will be named WinRM*
Check the newly created session with Get-PSSession:
I gave my session the name “ServerCore”. It connects to an Exchange 2019 server running on Server 2019 Core with hostname “exchange2019”
If you have more sessions you can go for the computer name:
Get-PSSession -ComputerName NameOfRemoteComputer
… or explicit for the session name:
Get-PSSession -Name NameOfSession
… and also for the Id:
Get-PSSession -Name ID
Enter a session directly as an interactive session with Enter-PSSession:
Enter-PSSession -name NameOfSession.
As a result, PowerShell switches to the entered interactive session:
You can perform tasks on the target server via PowerShell from the localhost within this session now.
Enter-PSSession does not require a session that has been created with New-PSSession before.
Find more on how to run Remote Commands from a Remote PowerShell Session here.
Exit an interactive session with Exit-PSSession:
Exit-PSSessionInstead of Exit-PSSession, you can just type Exit as well. It will have the same effect.
Thereafter, the session remains open but you have left the interactive session into the localhost’s PowerShell:
Disconnect a session with Disconnect-PSSession:
But you can’t disconnect an interactive session that has been initiated with Enter-PSSession.
To disconnect a session run the command below from your localhost’s PowerShell:
Disconnect-PSSession -name NameOfSession
…or disconnect it based on the ID:
Disconnect-PSSession -Id ID
Further, you can disconnect all remote session for a certain remote host based on ComputerName:
Disconnect-PSSession -Id ComputerName
You can later reconnect by using the Connect-PSSession cmdlet.
Reconnect a disconnected PowerShell session:
Connect-PSSession -name NameOfSession… or you connect it with its ID:
Connect-PSSession -Id ID… as well as with ComputerName:
Connect-PSSession -Id Computername
Remote PowerShell Session to Exchange Server
To connect with a Remote PowerShell Session to Exchange use:
$ExSession = New-PSSession -ConfigurationName Microsoft.Exchange –Name ExchangeSession -ConnectionUri “http://RemoteExchangeFQDN/powershell” -Credential $UserCred -Authentication Kerberos
You can verify the session with name ExchangeSession you just openend with Get-PSSession:
The same can be done from an Exchange Management Shell to connect through a Remote PowerShell session to another Exchange Server:
If you are on a Server Core type LaunchEMS into the command window.
Use the CMDlets from above
To display the output of a PowerShell command completely without ellipses take a look here: