I tried reg add
but it keeps saying invalid syntax
. Does anyone know how to edit the registry using .bat
files?
1 Answer
reg add "HKLM\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" /v PortNumber /t REG_DWORD /d <newportnumber>
A few things to note:
- How to Change the Listening Port for Remote Desktop gives the path incorrectly. It specifies the value name as part of the key.
- The key path contains a space. Therefore, the key name needs to be quoted (or otherwise escaped). Failing to do so will cause
reg.exe
to treatServer\WinStations\RDP-Tcp
as another parameter, without the required parameter flag, which is likely what caused your error. - Even though the value already exists, you must specify
REG_DWORD
as the type, otherwise, it will be overwritten asREG_SZ (string)
. - The
reg.exe
tool seems to default to using hex, rather than decimal. I'm not sure whether, if you were to specify a port of2000
, it would treat that as decimal2000
or as0x2000
(decimal 8192). It might only use hex if you prefix the number with0x
, but you should experiment. - This registry key is not writable by non-Administrators. You will need to run your script elevated, unless you change the privileges on the key.
- You will need to restart the Terminal Services service, and possibly some related services, after changing the port.
- The command line for this is
sc stop <servicename> && sc start <servicename>
. - The easier way to do this is probably just to restart, which is
shutdown /g
.- The
/g
means to reboot and come back to the current user and as potentially some of the current programs; use/r
instead for a simple reboot. - By default,
shutdown
gives you 30 seconds before the machine shuts down / reboots, and also will not reboot if any program resists closing (perhaps because of unsaved files). You can use the/t <seconds>
parameter to tell it to restart after some other number of seconds, and you can use/f
to force it to restart even if programs don't want to exit. Using/t
with any number of seconds greater than 0 will imply/f
as well.
- The
- The command line for this is
- You may need to update firewall and/or port forwarding rules after changing the RDP port, or the RDP server won't be reachable. Commands to do this will depend on your firewall and router, and it may not be practical to do from a script.
reg /?
orreg add /?