Guide

Remote PowerShell: how to connect to a remote PC with PowerShell

Remote PowerShell lets you run PowerShell commands on another Windows PC as if you were sitting at it. Turn it on once with Enable-PSRemoting on that PC, then connect with Enter-PSSession for an interactive prompt, or run commands on one or many PCs with Invoke-Command.

Below: the setup for PCs at home or in a small office (no domain), the commands, enabling Remote Desktop from PowerShell or Command Prompt, and a way to run PowerShell on your PCs over the internet with nothing to set up on the network.

  • Enable-PSRemoting
  • Enter-PSSession and Invoke-Command
  • Enable Remote Desktop from the command line

Updated · Works on Windows 10 and 11

How to set up remote PowerShell

PowerShell remoting uses the Windows Remote Management service (WinRM), which listens on port 5985 (HTTP) and 5986 (HTTPS). Even over HTTP, the session itself is encrypted once you've signed in. You need an administrator account on the PC you connect to.

  1. Step 1: Turn on remoting on the remote PC

    On the PC you want to reach, open PowerShell as administrator and run:

    Enable-PSRemoting -Force

    It starts WinRM, sets it to start with Windows and adds the firewall rules. If it fails because the network is set to Public, change it to Private in Settings, Network & internet, or add -SkipNetworkProfileCheck, which allows connections only from the same local network.

  2. Step 2: Trust the PC on your side (no domain only)

    On a company domain, skip this. At home or in a small office, tell the PC you connect from that the other PC may be trusted. In PowerShell as administrator:

    Set-Item WSMan:\localhost\Client\TrustedHosts -Value "office-pc" -Concatenate -Force
  3. Step 3: Test the connection

    Test-WSMan office-pc

    A reply with protocol versions means WinRM is answering. An error usually means the firewall or the name; try the PC's IP address instead.

  4. Step 4: Connect

    Enter-PSSession -ComputerName office-pc -Credential office-pc\alex

    Type the password when asked. The prompt changes to [office-pc]: PS>, and everything you type runs on that PC. Exit-PSSession brings you back.

Run commands on several PCs with Invoke-Command

Invoke-Command runs a command or a script on one or more PCs and brings the results back, each line labeled with the PC it came from:

Invoke-Command -ComputerName pc1, pc2, pc3 -ScriptBlock { Get-Service Spooler }
Invoke-Command -ComputerName pc1, pc2 -FilePath C:\Scripts\cleanup.ps1

Add -Credential as above when the PCs aren't in a domain. Older batch files run the same way; see how to write and run a Windows batch script.

Over the internet?

WinRM is meant for your own network. Forwarding port 5985 or 5986 on a router opens it to everyone on the internet, so don't. The last section has a way that needs no open port.

Enable Remote Desktop with PowerShell or Command Prompt

On Windows Pro, Enterprise or Education, these two lines turn on Remote Desktop and allow it through the firewall. Run them in PowerShell as administrator, on the PC itself or inside Enter-PSSession:

Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -Name fDenyTSConnections -Value 0
Enable-NetFirewallRule -DisplayGroup "Remote Desktop"

The same in Command Prompt as administrator:

reg add "HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 0 /f
netsh advfirewall firewall set rule group="remote desktop" new enable=Yes

To require Network Level Authentication, which is the safe default, add this:

Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp' -Name UserAuthentication -Value 1

Then connect from Command Prompt with mstsc /v:office-pc, or mstsc /v:office-pc /f for full screen. The Settings way, and what Windows Home can't do, is in how to enable Remote Desktop on Windows 11 and 10.

Turn remote PowerShell off again

Disable-PSRemoting -Force
Stop-Service WinRM
Set-Service WinRM -StartupType Disabled
Disable-NetFirewallRule -DisplayGroup "Windows Remote Management"

Run these as administrator on the PC that accepted connections.

Run PowerShell on your PCs without WinRM

RemoteAccessApp can run a PowerShell, Command Prompt or VBScript command on one PC or many from its Devices page, without opening a remote session. There's nothing to enable on the PC beyond the app, no port to open and no domain needed, and it works when the PC is on another network.

  • Select one or more PCs on the Devices page, right-click and choose Run a command. Pick PowerShell and type the command.
  • Each PC shows its output and exit code. Open command prompt gives you an interactive PowerShell window instead.
  • Commands run as the person signed in to that PC, with their everyday rights, so commands that need administrator rights won't work this way.
  • Unless unattended access lets you in, the person at the PC sees the exact command and chooses whether it runs.

The steps are in run a command without a remote session, and run a script on multiple PCs covers longer scripts, and schedules with Pro.

Questions, answered

Related