Skip to content

Instantly share code, notes, and snippets.

@asheroto
Last active December 26, 2023 07:13
Show Gist options
  • Save asheroto/530748b3bf0528cc4805d652b612f81f to your computer and use it in GitHub Desktop.
Save asheroto/530748b3bf0528cc4805d652b612f81f to your computer and use it in GitHub Desktop.
Enable RDP on a computer with PowerShell.

Enable RDP on a computer with PowerShell

The script performs the following:

  • Enables RDP access
  • Enables UDP support (as well as TCP)
  • Enables user authentication
  • Configures Windows Firewall

Notes:

  • Script must run on the computer you want to access
  • You can connect to a computer using Enter-PSSession
  • Script must run as Administrator
  • Firewall rules allow any profile
  • Port 3389 (default RDP port) is used
# Warning
Clear-Host
Write-Output "Run this script on the computer you want to access via RDP"
Write-Output ""
# Ask
Write-Output "Remote address can be an IP address or network with CIDR"
Write-Output "Example: 192.168.0.5 or 192.168.0.0/24"
Write-Output ""
$RemoteAddress = Read-Host "Remote Address"
Write-Output ""
# Registry
Set-ItemProperty -Path "HKLM:\System\CurrentControlSet\Control\Terminal Server"-Name "fDenyTSConnections" -Value 0
Set-ItemProperty -Path "HKLM:\Software\Policies\Microsoft\Windows NT\Terminal Services\Client" -Name "fClientDisableUDP" -Value 0
Set-ItemProperty -Path "HKLM:\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" -Name "UserAuthentication" -Value 1
# Firewall
New-NetFirewallRule -DisplayName "RDP (TCP)" -Direction Inbound -Action Allow -Protocol TCP -LocalPort 3389 -RemoteAddress $RemoteAddress -Profile Any -Enabled True | Out-Null
New-NetFirewallRule -DisplayName "RDP (UDP)" -Direction Inbound -Action Allow -Protocol UDP -LocalPort 3389 -RemoteAddress $RemoteAddress -Profile Any -Enabled True | Out-Null
# Result
Write-Output "Done! Now restart the computer"
Write-Output ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment