In this guide, we’ll show you how to change the Remote Desktop (RDP) port via PowerShell on Windows (e.g. a Windows Server), adjust the Windows Firewall accordingly, and restart the Remote Desktop service.
To begin, open Windows PowerShell as Administrator (right‑click “Windows PowerShell” in the Start menu → “Run as administrator”) and paste the following script:
# Port 33445 is just an example port. You can use any free ports you want.
$newPort = 33445
$regPath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp'
Set-ItemProperty -Path $regPath -Name 'PortNumber' -Value $newPort
if (-not (Get-NetFirewallRule -DisplayName "RDP Port $newPort" -ErrorAction SilentlyContinue)) {
New-NetFirewallRule `
-DisplayName "RDP Port $newPort" `
-Direction Inbound `
-Protocol TCP `
-LocalPort $newPort `
-Action Allow
}
Restart-Service -Name TermService -Force
Write-Host "The RDP port has been changed."
What this does:
-
Defines your new RDP port in the
$newPort
variable. -
Updates the Registry key at
HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp\PortNumber
. -
Creates a matching Windows Firewall rule (if it doesn’t already exist) to allow inbound TCP on your chosen port.
-
Restarts the Remote Desktop service (
TermService
) so the change takes effect.
After running the script, all active RDP sessions will be disconnected due to the service restart. To reconnect, open your RDP client and enter:
<Server-IP>:<New-Port>
For example: