Skip to content

Instantly share code, notes, and snippets.

@Aldaviva
Last active July 30, 2024 02:54
Show Gist options
  • Save Aldaviva/7800692ce755bf86558636353a1a3e54 to your computer and use it in GitHub Desktop.
Save Aldaviva/7800692ce755bf86558636353a1a3e54 to your computer and use it in GitHub Desktop.
Easy PowerShell Remoting

Easy PowerShell Remoting

PowerShell Remoting allows you to connect to a remote shell on another Windows computer.

The server is the Windows computer that will host the shell, and that you will connect to. The client is the Windows computer that will create the connection, on which you will see and interact with the shell.

These steps assume a workgroup with Windows 10 or Server 2016 or later on the server, and Windows 7 or later on the client. It assumes that the client is running with a user account that also exists on the server with the same password. Neither the server nor the client need to be joined to a domain or using Kerberos.

Prerequisites

  • Workgroup, not domain
  • Server operating system is Windows 10, Server 2016, or later
  • Client operating system is Windows 7, Server 2008 R2, or later
  • Client's user account also exists on server with same username and password

Setup

Run these commands in administrator PowerShells.

Server

  1. Turn on PowerShell Remoting. This automatically configures WinRM service, firewall, and other components).
    Enable-PSRemoting
  2. Trust your client computer, or trust any client computer.
    # trust client's hostname
    Set-Item wsman:\localhost\client\trustedhosts myclienthostname
    # or trust client's static IP address
    Set-Item wsman:\localhost\client\trustedhosts 192.168.1.100
    # or trust any client
    Set-Item wsman:\localhost\client\trustedhosts *
  3. Restart the WinRM service for trust settings to take effect.
    Restart-Service WinRM
  4. Add the user account to the group that allows remote sessions.
    1. Open Local Users and Groups (lusrmgr.msc).
    2. Make the desired user a Member Of the Remote Management Users group.

Client

  1. Trust your server computer, or trust any server computer.
    # trust server's hostname
    Set-Item wsman:\localhost\client\trustedhosts myserverhostname
    # or trust server's static IP address
    Set-Item wsman:\localhost\client\trustedhosts 192.168.1.101
    # or trust any server
    Set-Item wsman:\localhost\client\trustedhosts *
  2. When it prompts you to ask if you're sure, or if you want to start the WSMan service, press y.

If you get InvalidOperationException errors when you run this Set-Item cmdlet, try running Restart-Service WinRM.

Connection

Testing

Ensure the server's WinRM server is accessible to the client.

Test-WSMan myserverhostname

This should print out an object with wsmid and other properties.

Manual connection

Connect to a remote session.

Enter-PSSession myserverhostname

Quick connections

PowerShell alias

This will allow you to easily launch a remote session by typing the token of your choice.

  1. Create a new empty PowerShell profile file, if it doesn't already exist, in %USERPROFILE%\Documents\PowerShell\Microsoft.PowerShell_profile.ps1 (for PowerShell Core), or %USERPROFILE%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 (for Windows PowerShell).
  2. Create a function to enter the session, and an alias for that function.
    function Enter-PSSession-MyServer { Enter-PSSession myserverhostname }
    Set-Alias -Name myserver -Value Enter-PSSession-MyServer
  3. Open a PowerShell window and type myserver to connect.

Shortcut file

This will allow you to easily launch a remote session without having to type any commands.

  1. Create a new shortcut wherever you want, like the Start Menu or Desktop.
  2. Set the Target to
    powershell.exe -NoExit -Command "& Enter-PSSession myserverhostname"
@Aldaviva
Copy link
Author

Aldaviva commented Nov 5, 2022

See also SSH.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment