SaferComputing.com

Reference · Remote access

WinRM ports 5985 and 5986: check PowerShell remoting and turn it off

Check whether WinRM listens on ports 5985 and 5986 with Get-Service WinRM, winrm enumerate and Test-WSMan, see what Enable-PSRemoting changed, and turn PowerShell remoting off completely, beyond Disable-PSRemoting.

Published
Updated
Last checked
Applies to
Windows 11, Windows Server 2012 and later
Technical specification
Target artifactThe Windows Remote Management service (WinRM), its listeners on TCP 5985 for HTTP and 5986 for HTTPS, and the firewall rules that let other computers reach them. PowerShell remoting depends on this service.
Default behaviourMicrosoft documents the WinRM service as disabled by default on client versions of Windows, such as Windows 11, with no listener configured. On Windows Server 2012 and later, PowerShell remoting is on: the service starts automatically, and the firewall accepts connections on private and domain networks, and on public networks from the same subnet only. By default only members of the Administrators group can connect.
Safe stateOn computers nobody manages remotely: the service stopped and disabled, no listener, and the Windows Remote Management firewall rules off. Where remoting is needed: reachable only from your management network or a VPN, never from the internet, with Basic authentication and unencrypted traffic left off.
Check on WindowsGet-Service WinRM | Select-Object -Property Name, StartType, Status
Check on Windowswinrm enumerate winrm/config/listener, as Administrator
Check from another deviceTest-NetConnection -ComputerName ADDRESS -Port 5985 in PowerShell, then the same with -Port 5986
Fix on WindowsDisable-PSRemoting -Force as Administrator, then delete the listener, turn off the firewall rules and disable the service, as below
Fix at the routerdelete any port forward to 5985 or 5986

Read the result

Status and StartTypeWhat it means
Stopped and DisabledOff; no user or application can start it
Stopped and ManualNot running now, but a user, the Service Control Manager or an application can start it
RunningOn. Read the listeners next

On a Windows 11 computer, a running WinRM service means something turned it on, such as Enable-PSRemoting, winrm quickconfig or a management tool. Find out what before you turn it off: an IT provider may rely on it.

Each listener shows its address, transport and port. An address of * means any IP address on the computer; HTTP listens on 5985 and HTTPS on 5986. Without a listener, WinRM receives no requests even while the service runs. To see whether it answers, ask it:

Windows PowerShell
Test-WSMan

A reply listing the protocol version, product vendor and product version means the service is running and answering on this computer. From another computer on the same network, Test-WSMan -ComputerName NAME asks that computer instead. An error means it did not answer.

Why it matters

Anyone who can reach the port with the password of an account in the Administrators group can run PowerShell commands on the computer. Microsoft advises running Enable-PSRemoting only where it is needed, because it starts listeners. On public networks, the default firewall rule only admits computers on the same subnet; Microsoft says that rule is meant to protect the computer from potentially malicious outside connection attempts, and to take care before removing it. Nothing should reach 5985 or 5986 from the internet.

What Enable-PSRemoting changes

Run as Administrator, Enable-PSRemoting starts the WinRM service and sets its startup type to Automatic, creates a listener that accepts requests on any IP address, turns on a firewall exception for WS-Management, registers and enables the PowerShell session configurations and opens them to remote access, then restarts WinRM. On client versions of Windows, its firewall rules allow unrestricted remote access on private and domain networks; with -SkipNetworkProfileCheck, it also allows public networks, from the same local subnet only.

Turn it off completely

Disable-PSRemoting on its own only blocks remote access to the session configurations. Microsoft lists the other changes to undo by hand: stop and disable the WinRM service, delete the listener, turn off the firewall exceptions, and set LocalAccountTokenFilterPolicy back to 0. Delete the listener before you stop the service: Remove-WSManInstance works through WinRM itself. Run every step in PowerShell opened with Run as administrator.

1. Block the session configurations
Windows PowerShell, as Administrator
Disable-PSRemoting -Force

Sessions from other computers are refused from then on. Loopback sessions, from this computer to itself, still work.

2. Delete the listener
Windows PowerShell, as Administrator
Get-WSManInstance winrm/config/listener -Enumerate
Remove-WSManInstance -ResourceUri winrm/config/Listener -SelectorSet @{ Address = '*'; Transport = 'http' }

Use the Address and Transport values the first command printed, and repeat the second command for each listener, for example with 'https'. If Source reads GPO, Group Policy created the listener: change the Allow remote server management through WinRM policy instead, under Computer Configuration, Administrative Templates, Windows Components, Windows Remote Management (WinRM), WinRM Service. Some Microsoft documentation still calls the same policy Allow automatic configuration of listeners.

3. Turn off the firewall rules
Windows PowerShell, as Administrator
Get-NetFirewallRule -DisplayName 'Windows Remote Management*' | Select-Object Name, DisplayName, Enabled, Profile
Disable-NetFirewallRule -DisplayName 'Windows Remote Management*'

The built-in rules are called Windows Remote Management (HTTP-In). Microsoft's documentation names the one for public networks WINRM-HTTP-In-TCP-PUBLIC in one place and, in its example for client versions of Windows, WINRM-HTTP-In-TCP; the Profile column shows which rule covers public networks. Display names are translated on Windows installed in another language, and Microsoft notes that rule names can differ between Windows versions. A rule someone added for port 5986 has whatever name they gave it: list the inbound allow rules as on Open-port self-audit.

4. Stop and disable the service
Windows PowerShell, as Administrator
Stop-Service -Name WinRM
Set-Service -Name WinRM -StartupType Disabled
Get-Service WinRM | Select-Object -Property Name, StartType, Status

The last line should show Disabled and Stopped. If Stop-Service reports services that depend on WinRM, list them with Get-Service -Name WinRM | Format-List -Property Name, DependentServices before you add -Force.

5. Restore LocalAccountTokenFilterPolicy
Windows PowerShell, as Administrator
Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System -Name LocalAccountTokenFilterPolicy -ErrorAction SilentlyContinue

A value of 1 turns off User Account Control's remote restrictions for all users. Only if the command shows 1, set it back to 0, which limits remote access to members of the Administrators group:

Windows PowerShell, as Administrator
Set-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System -Name LocalAccountTokenFilterPolicy -Value 0

No output means the value was never set, and there is nothing to restore.

If remoting must stay on

Servers and managed computers

Allow 5985 and 5986 only from your management network or a VPN, never from the internet, and delete router port forwards to them. Check that nobody opened the public-network rule to every address; Any means the same-subnet restriction was removed. If the command reports that no rule has this name, use the Name of the rule whose Profile reads Public in step 3's list; Microsoft's example for client versions of Windows calls it WINRM-HTTP-In-TCP:

Windows PowerShell, as Administrator
Get-NetFirewallRule -Name 'WINRM-HTTP-In-TCP-PUBLIC' | Get-NetFirewallAddressFilter | Select-Object RemoteAddress
  • In winrm get winrm/config, keep the service settings AllowUnencrypted and Basic false, their defaults. Basic authentication sends the user name and password in clear text.
  • WinRM encrypts PowerShell remoting traffic after the first authentication, even over HTTP. HTTPS on 5986 adds TLS, and needs a Server Authentication certificate whose name matches the computer's host name and that is not expired, revoked or self-signed.
  • Members of the Administrators group can connect by default, and Microsoft also names the Remote Management Users group. Keep both groups short.

Troubleshooting

The service or listener comes back
  • The listener's Source reads GPO: a Group Policy creates it. See step 2.
  • A management tool turned it back on. Ask whoever manages the computer whether they need it.
Ports 80 and 443 answer as well

WinRM has compatibility listeners: with EnableCompatibilityHttpListener true it also listens on port 80, and with EnableCompatibilityHttpsListener true on port 443. Both are false by default; check them in the output of winrm get winrm/config.

Sources