Skip to content

Instantly share code, notes, and snippets.

@iyre
Last active November 28, 2021 05:36
Show Gist options
  • Save iyre/83e58b9494b4277666c12176bcbf2113 to your computer and use it in GitHub Desktop.
Save iyre/83e58b9494b4277666c12176bcbf2113 to your computer and use it in GitHub Desktop.
Remotely manipulate RDP port assignments with PowerShell
Function Edit-RdpPort {
<#
.SYNOPSIS
Changes the port assigned to RDP
.DESCRIPTION
Change the port assigned to RDP by updating the registry and restarting relevant services
.PARAMETER ComputerName
ComputerName that will have its port changed
.PARAMETER Port
New port number
.EXAMPLE
Edit-RDPPort -ComputerName 'Contoso1' -Port 7777
#>
Param(
[String]$ComputerName = $env:ComputerName,
[Int]$Port = 3389
)
If (-Not (Test-RdpPort -ComputerName $ComputerName -Port $Port)) {
Write-Host -ForegroundColor Red "The new port is not reachable."
Read-Host -Prompt "Press Enter to continue or Ctrl+C to quit"
}
Write-Host -NoNewline "Old port: "
Get-RdpPort -ComputerName $ComputerName
Set-RdpPort -ComputerName $ComputerName -Port $Port
Write-Host -NoNewline "New port: "
Get-RdpPort -ComputerName $ComputerName
Restart-RdpServices -ComputerName $ComputerName
}
Function Test-RdpPort {
<#
.SYNOPSIS
Tests the port assigned to RDP
.DESCRIPTION
Test the port assigned to RDP by attempting to connect
.PARAMETER ComputerName
ComputerName that will have port tested
.PARAMETER Port
TCP port to be tested
.EXAMPLE
Test-RDPPort -ComputerName 'Contoso1' -Port 7777
#>
Param(
[String]$ComputerName = $env:ComputerName,
[Int]$Port = (Get-RdpPort -ComputerName $ComputerName)
)
$ErrorActionPreference = "silentlycontinue"
$Client = New-Object System.Net.Sockets.TcpClient
$Client.Connect($ComputerName,$Port)
Return $Client.Connected
}
Function Set-RdpPort {
<#
.SYNOPSIS
Sets the port assigned to RDP
.DESCRIPTION
Set the port assigned to RDP by writing to the registry
.PARAMETER ComputerName
ComputerName that will have its port changed
.PARAMETER Port
New port number
.EXAMPLE
Set-RDPPort -ComputerName 'Contoso1' -Port 7777
#>
Param(
[String]$ComputerName = $env:ComputerName,
[Int]$Port = 3389
)
$Path = "Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp"
Invoke-Command -ComputerName $ComputerName -ScriptBlock { Set-ItemProperty -Path $Args[0] -Name "PortNumber" -Value $Args[1] } -ArgumentList $Path,$Port
}
Function Get-RdpPort {
<#
.SYNOPSIS
Gets the port assigned to RDP
.DESCRIPTION
Get the port assigned to RDP by looking in the registry
.PARAMETER ComputerName
ComputerName that will have its port checked
.EXAMPLE
Get-RDPPort -ComputerName 'Contoso1'
#>
Param([String]$ComputerName = $env:ComputerName)
$Path = "Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp"
$Config = Invoke-Command -ComputerName $ComputerName -ScriptBlock { Get-ItemProperty -Path $Args[0] } -ArgumentList $Path
Return $Config.PortNumber
}
Function Restart-RdpServices {
<#
.SYNOPSIS
Restarts services related to RDP
.DESCRIPTION
Restarts services related to RDP: 'TermService' and 'Remote Desktop Services UserMode Port Redirector'
.PARAMETER ComputerName
ComputerName that will have RDP related services restarted.
.EXAMPLE
Restart-RDP -ComputerName 'Contoso1'
#>
Param([string]$ComputerName = $env:ComputerName)
Get-Service -ComputerName $ComputerName -Name 'UmRdpService' | Stop-Service -Force
Get-Service -ComputerName $ComputerName -Name 'TermService' | Stop-Service -Force
Get-Service -ComputerName $ComputerName -Name 'TermService' | Start-Service
Get-Service -ComputerName $ComputerName -Name 'UmRdpService' | Start-Service
Return "Services restarted"
}
Function Edit-RdpPort {
<#
.SYNOPSIS
Changes the port assigned to RDP
.DESCRIPTION
Changes the port assigned to RDP by updating the registry and restarting relevant services
.PARAMETER Port
Specifies new port number. Defaults to 3389
.EXAMPLE
Edit-RdpPort -Port 7777
Old port: 3389
New port: 7777
.EXAMPLE
Edit-RdpPort 5656
The new port is not reachable.
Press Enter to continue or Ctrl+C to quit
#>
Param (
[Int]$Port = 3389
)
Process {
If (-Not (Test-RdpPort -Port $Port)) {
Write-Host -ForegroundColor Red "The new port is not reachable."
Read-Host -Prompt "Press Enter to continue or Ctrl+C to quit"
}
Write-Host "Old port: $(Get-RdpPort)"
Test-RdpPort
Set-RdpPort -Port $Port
Write-Host "New port: $(Get-RdpPort)"
Test-RdpPort
Restart-RdpServices
}
}
Function Test-RdpPort {
<#
.SYNOPSIS
Tests the port assigned to RDP
.DESCRIPTION
Tests the port assigned to RDP by attempting to connect
.PARAMETER Port
Specifies TCP port to be tested. Defaults to current RDP port
.EXAMPLE
Test-RdpPort -Port 7777
#>
Param (
[Int]$Port = (Get-RdpPort)
)
Process {
$ErrorActionPreference = "SilentlyContinue"
$Client = New-Object -TypeName System.Net.Sockets.TcpClient
$Client.Connect("localhost", $Port)
$Client.Connected
}
}
Function Set-RdpPort {
<#
.SYNOPSIS
Sets the port assigned to RDP
.DESCRIPTION
Set the port assigned to RDP by writing to the registry
.PARAMETER Port
Species new port number. Defaults to 3389
.EXAMPLE
Set-RdpPort -Port 7777
#>
Param (
[Int]$Port = 3389
)
Process {
$Path = "Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp"
Set-ItemProperty -Path $Path -Name "PortNumber" -Value $Port
}
}
Function Get-RdpPort {
<#
.SYNOPSIS
Gets the port assigned to RDP
.DESCRIPTION
Get the port assigned to RDP by looking in the registry
#>
Process {
$Path = "Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp"
$Config = Get-ItemProperty -Path $Path
$Config.PortNumber
}
}
Function Restart-RdpServices {
<#
.SYNOPSIS
Restarts services related to RDP
.DESCRIPTION
Restarts services related to RDP: 'TermService' and 'Remote Desktop Services UserMode Port Redirector'
#>
Process {
Get-Service -Name "UmRdpService" | Stop-Service -Force
Get-Service -Name "TermService" | Stop-Service -Force
Get-Service -Name "TermService" | Start-Service
Get-Service -Name "UmRdpService" | Start-Service
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment