Disable-RDPRemotely.ps1


Description

Purpose

Disables Remote Desktop Protocol (RDP) on remote computers.

Detailed Description

The Disable-RDPRemotely function disables Remote Desktop Protocol (RDP) on one or more remote computers. It uses either CIM or WMI to perform the operation, depending on the version of PowerShell.

Back to Top

Usage

Example 1

Disable-RDPRemotely -ComputerName 'Server01', 'Server02'

Disables RDP on the computers ‘Server01’ and ‘Server02’.

Example 2

'Desktop01', 'Desktop02' | Disable-RDPRemotely

Disables RDP on the computers ‘Desktop01’ and ‘Desktop02’ using pipeline input.

Back to Top

Notes

  • This function requires administrative privileges on the remote computers.

  • If the computer is running PowerShell version 6 or later, CIM is used to disable RDP. Otherwise, WMI is used.

  • For more information, visit the help URI: http://scripts.lukeleigh.com/

Back to Top


Script

<#
.SYNOPSIS
Disables Remote Desktop Protocol (RDP) on remote computers.

.DESCRIPTION
The Disable-RDPRemotely function disables Remote Desktop Protocol (RDP) on one or more remote computers. It uses either CIM or WMI to perform the operation, depending on the version of PowerShell.

.PARAMETER ComputerName
Specifies the name of the computer(s) on which to disable RDP. This parameter accepts an array of strings, allowing you to specify multiple computer names.

.INPUTS
None. You cannot pipe input to this function.

.OUTPUTS
System.String. The function returns a string indicating the success or failure of the operation.

.NOTES
- This function requires administrative privileges on the remote computers.
- If the computer is running PowerShell version 6 or later, CIM is used to disable RDP. Otherwise, WMI is used.
- For more information, visit the help URI: http://scripts.lukeleigh.com/

.EXAMPLE
Disable-RDPRemotely -ComputerName 'Server01', 'Server02'
Disables RDP on the computers 'Server01' and 'Server02'.

.EXAMPLE
'Desktop01', 'Desktop02' | Disable-RDPRemotely
Disables RDP on the computers 'Desktop01' and 'Desktop02' using pipeline input.

#>
function Disable-RDPRemotely {
    [CmdletBinding(DefaultParameterSetName = 'Default',
        ConfirmImpact = 'Medium',
        SupportsShouldProcess = $true,
        HelpUri = 'http://scripts.lukeleigh.com/')]
    [OutputType([string], ParameterSetName = 'Default')]
    param
    (
        [Parameter(ParameterSetName = 'Default',
            Mandatory = $false,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            ValueFromRemainingArguments = $true,
            HelpMessage = 'Enter the Name of the computer you would like to connect to.')]
        [Alias('cn')]
        [string[]]
        $ComputerName
    )
    foreach ($Computer in $ComputerName) {
        # Disable RDP using CIM
        if ($PSVersionTable.PSVersion.Major -ge 6) {
            $Win32TerminalServiceSettings = Get-CimInstance -Namespace root/cimv2/TerminalServices -ClassName Win32_TerminalServiceSetting -ComputerName $Computer
            $Win32TerminalServiceSettings | Invoke-CimMethod -MethodName SetAllowTSConnections -Arguments @{AllowTSConnections = 0; ModifyFirewallException = 0 } -ComputerName $Computer
        }
        # Disable RDP using WMI
        else {
            $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, simply 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