2

I tried reg add but it keeps saying invalid syntax. Does anyone know how to edit the registry using .bat files?

1
  • 1
    Correct syntax can be found via reg /? or reg add /?
    – JW0914
    Commented Dec 8, 2019 at 13:39

1 Answer 1

5
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 treat Server\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 as REG_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 of 2000, it would treat that as decimal 2000 or as 0x2000 (decimal 8192). It might only use hex if you prefix the number with 0x, 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.
  • 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.
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .