Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


May 2008

Enable Remote Desktop Over a Network On Demand

Use simple .cmd scripts to check a computer's remote connection setting, turn Remote Desktop on, and turn it back off.
RSS
Subscribe to Windows IT Pro | See More Systems Administration Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

Remote Desktop is one of the most important support tools for administrators of remote computers, but it’s often not enabled on desktop systems when you need it. If you know what you’re looking for, however, it’s fairly easy to enable it remotely on a network.

The Remote Desktop service on desktop systems is always running, even if remote access is disabled. Whether remote connections are enabled or disabled is controlled by a value named fDenyTSConnections under the registry key HKEY_LOCAL_ MACHINE\SYSTEM\Current- ControlSet\Control\Terminal Server. By default, this value is 1 (which is disabled). When you use the Remote tab in the System Properties dialog box to enable Remote Desktop access (shown in Figure 1 for a computer running Windows Vista), this value changes to 0.

Because you can enable remote connections through the registry, you have a wide range of options for modifying Remote Desktop access. Essentially, any technique for accessing a registry value will work, including the following:

  • You can open regedit, connect to the remote computer, then change the value.
  • You can have a PC import a .reg file on startup.
  • You can use Windows Management Instrumentation’s (WMI’s) System Registry provider to make the change from a script.
  • You can use reg.exe to make the change from the command line or a script.

Many administrators seem to need to enable Remote Desktop access on the fly. It’s also usually a good practice to turn the remote connection back off when you’re done using it. So, to simplify the process of checking whether Remote Desktop is enabled, enabling it if it’s not, and disabling it after you’re done using it, I wrote three one-line .cmd scripts: CheckRD.cmd, RDOn.cmd, and RDOff.cmd.

To check whether a computer has Remote Desktop enabled, you can run the CheckRD.cmd script in Listing 3. To download it and the other scripts, click the Download the Code Here button at the top of this article. CheckRD .cmd and the other two scripts take a single command-line argument: the name of the remote computer to connect to. For example, if you want to use CheckRD.cmd to see whether Remote Desktop is enabled on the remote computer named Client07, you’d run the command

CheckRD Client07

You can also use this script to check the setting on the local system. Just specify a period (.) as the name of the host computer. No matter whether you check a local or remote system, the script returns the value of fDenyTSConnections. If Remote Desktop is disabled, 0x1 is returned. If it’s enabled, 0x0 is returned.

To enable Remote Desktop, you can run RDOn.cmd, which Listing 4 shows. You need to run this script from an account that has sufficient privileges to change the remote machine’s registry. To enable access on the Client07 computer, you’d run the script with the command

RDOn Client07

You’ll be able to immediately make a remote connection.

If you need to disable Remote Desktop after you’re done using it, you can run RDOff.cmd in Listing 5. For example, to turn Remote Desktop off on the Client07 computer, you’d use the command

RDOff Client07

As you can see, CheckRD .cmd, RDOn.cmd, and RDOff .cmd are simple scripts. But don’t let their simplicity deceive you. These scripts let you easily enable and disable a computer’s remote connection on demand.

—Alex K.
Angelopoulos
Senior Network Engineer

End of Article



Reader Comments
::RemoteDesktopTool.bat Built by Steve Signorelli.
::Inspired by —Alex K. Angelopoulos and InstantDoc #98551

@ECHO OFF
:Prompt
::Prompting technician for machine name/IP which to connect
SET /P TargetMachine=Please enter the machine name or IP address:
CLS
ECHO Remote Desktop Check on \\%TargetMachine%
:Menu
ECHO.
ECHO 1. Remote Desktop Check (Enabled 0x0 or disabled 0x1)
ECHO 2. Remote Desktop On (0x0)
ECHO 3. Remote Desktop Off (0x1)

set choice=
set /p choice=Please type in your choice and press enter.
if not '%choice%'==' set choice=%choice:~0,1%
if '%choice%'=='1' goto RemoteDesktopCheck
if '%choice%'=='2' goto RemoteDesktopOn
if '%choice%'=='3' goto RemoteDesktopOff
ECHO "%choice%" is not valid please try again
ECHO.
goto Menu

::Listing 3: CheckRD.cmd
:RemoteDesktopCheck
@reg query "\\%TargetMachine%\HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections
GOTO Prompt

::Listing 4: RDOn.cmd
:RemoteDesktopOn
@reg add "\\%TargetMachine%\HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 0 /f
GOTO Prompt

::Listing 5: RDOff.cmd
:RemoteDesktopOff
@reg add "\\%TargetMachine%\HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 1 /f
GOTO Prompt

stevesig May 02, 2008 (Article Rating: )


This is impressive, Steve. I had considered integrating everything into one script, but didn't for one simple reason: I was too lazy. :)
The prompting was a nice touch as well. I normally write scripts so that they work quietly as part of a toolchain, but your technique is much more appropriate since the script is really about a task performed for a single machine during remote support.

AlexKAngelopoulos May 06, 2008 (Article Rating: )


You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
Microsoft, News Corp. Discuss Locking Out Google

Microsoft and Rupert Murdoch's News Corp. recently discussed an alliance that would counter Google's fledgling online news service. ...

2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...


Related Articles Tips for Using Remote Desktop Connections

Open Up Your Remote Connections

Remote Desktop--A Cure for the PC Shuffle

Getting Started: Remote Administration

Scripting Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Related Events Deep Dive into Windows Server 2008 R2 presented by John Savill

7 Ways To Get More From Your SharePoint Deployment Now

Configuration Manager SP1 and R2 Overview

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement