Disable-RDPRemotelyWMI.ps1


Description

Disable-RDPRemotelyWMI uses the classic WMI Win32_TerminalServiceSetting class to switch off Remote Desktop access on one or more remote computers. Supply computer names via the -ComputerName parameter or pipeline input and the function calls SetAllowTSConnections(0,0) to remove both RDP access and the firewall hole.

Usage examples

Disable-RDPRemotelyWMI -ComputerName 'srv-core01'
'srv-a','srv-b' | Disable-RDPRemotelyWMI

Choose the CIM variant when PowerShell Remoting and the WSMan stack are available; this WMI helper remains useful for legacy hosts.


Script

function Disable-RDPRemotelyWMI {
    # You can also disable it using the below method.
    [CmdletBinding()]
    param (
        [Parameter()]
        [string[]]
        $ComputerName
    )
    foreach ($Computer in $ComputerName) {
        $tsobj = Get-WmiObject -Class Win32_TerminalServiceSetting -Namespace Root\CimV2\TerminalServices -ComputerName $Computer
        $tsobj.SetAllowTSConnections(0, 0)
    }
}

Back to Top


Download

Please feel free to copy parts of the script or if you would like to download the entire script, simple click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.


Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.

Issue


Back to Top