修改Windows远程桌面端口并配置防火墙默认情况下,Windows远程桌面使用3389端口。为了增强安全性,建议修改默认端口,并配置防火墙允许新端口的连接请求。## PowerShell脚本powershell$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())if ($currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) -eq $false) { Start-Process powershell.exe -Verb RunAs -ArgumentList ('-NoExit -NoProfile -ExecutionPolicy UnRestricted -File '{0}'' -f ($myinvocation.MyCommand.Definition)) exit $LASTEXITCODE}# Running with Full Privileges$Host.UI.RawUI.WindowTitle='自定义 Windows 远程桌面服务端口''读取远程桌面服务监听端口'Get-ItemProperty -Path 'HKLM:/SYSTEM/CurrentControlSet/Control/Terminal Server/WinStations/RDP-Tcp' -name 'PortNumber''是否要重新设置远程桌面服务监听端口?任意键继续,取消请关闭。'[Console]::Readkey() | Out-Null[ValidateRange(1025,65535)]$portvalue = Read-Host '请输入新的远程桌面监听端口(大于1024且小于65536)'Set-ItemProperty -Path 'HKLM:/SYSTEM/CurrentControlSet/Control/Terminal Server/WinStations/RDP-Tcp' -name 'PortNumber' -Value $portvalue '远程桌面服务监听端口设置成功!''在网络防火墙中添加远程桌面监听端口……'New-NetFirewallRule -DisplayName 'Remote Desktop - (TCP-In)' -Description '远程桌面服务允许 RDP 通信的入站规则。' -Group '@FirewallAPI.dll,-28752' -Profile Any -Direction Inbound -Action Allow -Protocol TCP -LocalPort $portvalue -Program 'C:/Windows/System32/svchost.exe'New-NetFirewallRule -DisplayName 'Remote Desktop - (UDP-In)' -Description '远程桌面服务允许 RDP 通信的入站规则。' -Group '@FirewallAPI.dll,-28752' -Profile Any -Direction Inbound -Action Allow -Protocol UDP -LocalPort $portvalue -Program 'C:/Windows/System32/svchost.exe''网络防火墙端口设置成功!''重启远程桌面服务使配置生效……'Restart-Service -Name TermService -Force'远程桌面服务重启成功,新端口设置已生效,按任意键退出!'[Console]::Readkey() | Out-Null## 批处理脚本batch@echo off:: Check if running as administratornet session >nul 2>&1if %errorlevel% neq 0 ( echo Running with limited privileges. Restarting as administrator... powershell -Command 'Start-Process cmd.exe -ArgumentList '/c %~nx0' -Verb RunAs' exit /b):: Running with full privilegestitle 自定义 Windows 远程桌面服务端口echo.echo 读取远程桌面服务监听端口reg query 'HKLM/SYSTEM/CurrentControlSet/Control/Terminal Server/WinStations/RDP-Tcp' /v 'PortNumber'echo.echo 是否要重新设置远程桌面服务监听端口?任意键继续,取消请关闭。pause >nulecho.set /p portvalue='请输入新的远程桌面监听端口(大于1024且小于65536): 'echo.reg add 'HKLM/SYSTEM/CurrentControlSet/Control/Terminal Server/WinStations/RDP-Tcp' /v 'PortNumber' /t REG_DWORD /d %portvalue% /fecho.echo 远程桌面服务监听端口设置成功!echo.echo.echo 在网络防火墙中添加远程桌面监听端口……netsh advfirewall firewall add rule name='Remote Desktop - (TCP-In)' description='远程桌面服务允许 RDP 通信的入站规则。' group='@FirewallAPI.dll,-28752' profile=any direction=in action=allow protocol=tcp localport='%portvalue%' program='C:/Windows/System32/svchost.exe'netsh advfirewall firewall add rule name='Remote Desktop - (UDP-In)' description='远程桌面服务允许 RDP 通信的入站规则。' group='@FirewallAPI.dll,-28752' profile=any direction=in action=allow protocol=udp localport='%portvalue%' program='C:/Windows/System32/svchost.exe'echo.echo 网络防火墙端口设置成功!echo.echo 重启远程桌面服务使配置生效……net stop TermService >nul 2>&1net start TermService >nul 2>&1echo.echo 远程桌面服务重启成功,新端口设置已生效,按任意键退出!pause >nul## 注意* 修改远程桌面端口后,需要使用 新端口 连接远程桌面。* 请妥善保管好您的新端口号,避免忘记。

修改Windows远程桌面端口并配置防火墙

原文地址: https://www.cveoy.top/t/topic/bKDr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录