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 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="

Step 3. Restart WinRM Service

Run the following command to restart the WinRM service now:

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):

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


How to Create a PowerShell Session on a Remote Computer

5 thoughts on “How to Create a PowerShell Session on a Remote Computer

  • Permalink

    winrm set winrm/config/client ‘@{TrustedHosts=”

    absolutely does not work!
    You end up with a newline and to right-pointing arrows 🙂

    • Permalink

      winrm set winrm/config/client ‘@{TrustedHosts=”*”}’

      • Permalink

        * as TrustedHosts would be a security risk. Best would be:
        winrm set winrm/config/client ‘@{TrustedHosts=””}’

  • Permalink

    winrm set winrm/config/client ‘@{TrustedHosts=”.. try this 🙂

  • Permalink

    If Enable-PSRemoting -Force is not executed then what error should i expect ?

    It will throw
    New-PSSession : Connecting to remote server failed with the following error message : WinRM cannot process the request. The following error with errorcode 0x80090311 occurred while using Kerberos authentication ?

    To use kerberos authentication do i need to import activedirectory module in powershell?

    can I user kerberos authentication here instead basic ?
    I am


Leave a Reply

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