To execute PowerShell commands or scripts on a remote computer, you need to create a session. This is also known as PowerShell remoting and it is just like an SSH session to an operating system. There are several ways to create a PowerShell session. I'll list one of the easiest methods here. These are the required steps.

Step 1. Enabling PowerShell Remoting

Open the PowerShell with administrative privileges on the remote computer and execute the following command:

Enable-PSRemoting -Force

This command starts the WinRM service and creates a firewall rule to allow incoming connections. The -force option avoids PowerShell to prompt you for a confirmation at each step.

Step 2. Configure TrustedHosts

On both computers, configure the TrustedHosts setting so they know each other. Execute the following command on both computers to do so:

winrm set winrm/config/client '@{TrustedHosts="<Computername or IP address of other computer"}'

Step 3. Restart WinRM Service

Restart-Service WinRM

Step 4. Test the Connection

To test your configuration, execute the following command on the local computer:

Test-WsMan <RemoteCOMPUTERName>

For the configuration to be successful, this command should execute without errors and show the information of the WinRM service of the remote computer.

Create a PowerShell Session and Execute Commands

Now when both computers have been configured, you can create a session using the following commands (Execute these commands with elevated privileges):

$cred=Get-Credential
$sess = New-PSSession -Credential $cred -ComputerName <remotemachinename>
Enter-PSSession $sess
<Run commands in remote session>
Exit-PSSession
Remove-PSSession $sess

 

How to Create a PowerShell Session on a Remote Computer

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Ad #native_company# — #native_desc# #native_cta#