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

$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

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

  • August 14, 2019 at 2:14 pm
    Permalink

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

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

    Reply
    • October 11, 2019 at 2:39 pm
      Permalink

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

      Reply
      • February 20, 2020 at 11:29 am
        Permalink

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

        Reply
  • September 12, 2019 at 11:07 am
    Permalink

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

    Reply
  • September 18, 2019 at 7:18 pm
    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

    Reply
  • April 9, 2020 at 5:15 pm
    Permalink

    winrm help config

    PS C:\WINDOWS\system32> winrm set winrm/config/client ‘@{TrustedHosts=””}’
    Client
    NetworkDelayms = 5000
    URLPrefix = wsman
    AllowUnencrypted = false
    Auth
    Basic = true
    Digest = true
    Kerberos = true
    Negotiate = true
    Certificate = true
    CredSSP = false
    DefaultPorts
    HTTP = 5985
    HTTPS = 5986
    TrustedHosts =

    Reply
    • April 9, 2020 at 5:16 pm
      Permalink

      ‘@{TrustedHosts=””}’

      Reply
  • April 16, 2020 at 6:56 pm
    Permalink

    Thanks a lot for this tip that helps me Karim ! 😉
    It works perfectly.

    I can now launch remote processes with user credential, for example : ” Start-Process Notepad”

    To do this test, I logged a user session to this machine with the same credential to see what’s happen.
    So .. : I see NOTEPAD running in task manager.
    But I didn’t see the NOTEPAD GUI interface , running on the remote machine.
    I only know that it runs with task manager.

    Can you explain me what I have to do to see the whole NOTEPAD GUI app ?

    Thank you

    Reply

Leave a Reply

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

*