Set-RDPRemotely.ps1
19 Sep 2025Description
Purpose
No synopsis provided.
Detailed Description
No detailed description provided.
Usage
No usage examples provided.
Notes
No additional notes.
Script
function Set-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,
[Parameter(ParameterSetName = 'Default',
Mandatory = $true,
HelpMessage = 'Specify whether to enable or disable RDP.')]
[ValidateSet('Enable', 'Disable')]
[string]
$State
)
begin {}
process {
foreach ($Computer in $ComputerName) {
if ($PSCmdlet.ShouldProcess("$Computer", "Setting RDP to $State")) {
# Get the remote computer's operating system version
$OSVersion = (Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $Computer).Version
# Determine the appropriate method to use based on the operating system version
if ([version]$OSVersion -ge [version]'6.0') {
# Enable or disable RDP using CIM
$Win32TerminalServiceSettings = Get-CimInstance -Namespace root/cimv2/TerminalServices -ClassName Win32_TerminalServiceSetting -ComputerName $Computer
$Win32TerminalServiceSettings | Invoke-CimMethod -MethodName SetAllowTSConnections -Arguments @{AllowTSConnections = ($State -eq 'Enable'); ModifyFirewallException = ($State -eq 'Enable') } -ComputerName $Computer
}
else {
# Enable or disable RDP using WMI
$tsobj = Get-WmiObject -Class Win32_TerminalServiceSetting -Namespace Root\CimV2\TerminalServices -ComputerName $Computer
$tsobj.SetAllowTSConnections(($State -eq 'Enable'), ($State -eq 'Enable'))
}
# Output the computer name and current state
$RDPStatus = if ($State -eq 'Enable') { 'Enabled' } else { 'Disabled' }
$OutputObject = [PSCustomObject]@{
ComputerName = $Computer
RDPStatus = $RDPStatus
}
Write-Output $OutputObject
}
}
}
end {}
}
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.