Techibee.com

Use WMI & PowerShell to enable or disable RDP on Windows Server

The subject says it all. Many of the system admins know that flipping a registry key will enable or disable RDP connectivity on a Windows Server or desktop. However, modifying the registry is not always convenient. For those out there who thinks there should be much easier way, this post is for them πŸ™‚

You can enable RDP on a remote host by simply running the below two lines.

$tsobj = Get-WmiObject -Class Win32_TerminalServiceSetting -Namespace Root\CimV2\TerminalServices -ComputerName SERVER01
$tsobj.SetAllowTSConnections(1,1)

You can also disable it using the below method.

$tsobj = Get-WmiObject -Class Win32_TerminalServiceSetting -Namespace Root\CimV2\TerminalServices -ComputerName SERVER01
$tsobj.SetAllowTSConnections(0,0)

Do you care to check if it is currently enabled or disabled before acting? Use the below code.

$tsobj = Get-WmiObject -Class Win32_TerminalServiceSetting -Namespace Root\CimV2\TerminalServices -ComputerName SERVER01
$tsobj.AllowTSConnections

Wondering what are the 2 arguments for SetAllowTSConnections function? The first one represents AllowTSConnections(0 – disable, 1 – enable) and the second one represents ModifyFirewallException (0 – don’t modify firewall rules, 1 – modify firewall rules). You can read more about it at https://docs.microsoft.com/en-us/windows/win32/termserv/win32-terminalservicesetting-setallowtsconnections

Hope this article is helpful for you. Feel free to write in the comments section if you have any questions.

Exit mobile version