• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 830
  • Last Modified:

How to change computer name and IP from a batch file

Anyone know how to change the computer name and IP from a batch file. If this is not directly possible is there some freeware utility which I can call from a batch file passing the new computer name and IP?
0
CharlesSHill
Asked:
CharlesSHill
2 Solutions
 
KaarthickCommented:
Command to Change IP Address

netsh interface ip set address name="Local Area Connection" static 192.168.0.28 255.255.255.0 192.168.0.1 1
0
 
russhuntingtonCommented:
If you have support tools installed, you can use: "NETDOM RENAME COMPUTER targetComputer /NewName:newName". With older Windows operating systems, you can use "hostname -s".
0
 
KaarthickCommented:
The following link gives you some information regarding NETDOM which was suggested by "russhuntington"

http://support.microsoft.com/default.aspx?scid=kb;en-us;298593&sd=tech
0
 
robermCommented:
Below is part of a Perl script ive done used to change both IP and machine name as part of an automated configuration script. It basically changes the registry directly. I you do not wish to use Perl then u can simply use a .reg file, incorporating the registry keys in the script below. The .reg file can then be launched from within your .bat file.

At the bottom half is an example of a  .reg file for your purpose simply replace the computer name, and IP address with the ones u wish to use.

Hope this helps.


==============================================

use Win32::Registry;

#######------Change computer name in registry------######


      $key='System\CurrentControlSet\Control\ComputerName\ComputerName';
      Win32::Registry::RegOpenKeyEx
            (&HKEY_LOCAL_MACHINE,$key,NULL,&KEY_ALL_ACCESS,$RegHandle);
      Win32::Registry::RegSetValueEx
            ($RegHandle, 'ComputerName', NULL, &REG_SZ, $computername);
      Win32::Registry::RegCloseKey($RegHandle);
      
      $key='System\CurrentControlSet\Services\Tcpip\Parameters';
      Win32::Registry::RegOpenKeyEx
            (&HKEY_LOCAL_MACHINE,$key,NULL,&KEY_ALL_ACCESS,$RegHandle);
      Win32::Registry::RegSetValueEx
            ($RegHandle, 'ComputerName',NULL, &REG_SZ, $computername);
      Win32::Registry::RegCloseKey($RegHandle);
      
      $key='System\CurrentControlSet\Services\Tcpip\Parameters';
      Win32::Registry::RegOpenKeyEx
            (&HKEY_LOCAL_MACHINE,$key,NULL,&KEY_ALL_ACCESS,$RegHandle);
      Win32::Registry::RegSetValueEx
            ($RegHandle, 'Hostname',NULL, &REG_SZ, $computername);
      Win32::Registry::RegCloseKey($RegHandle);


######------Change the IP Address in the registry------######


      $key='System\CurrentControlSet\Services\El90x1\Parameters\Tcpip';
      Win32::Registry::RegOpenKeyEx
            (&HKEY_LOCAL_MACHINE,$key,NULL,&KEY_ALL_ACCESS,$RegHandle);
      Win32::Registry::RegSetValueEx
            ($RegHandle, 'IPAddress',NULL, &REG_MULTI_SZ, $finalip);
      Win32::Registry::RegCloseKey($RegHandle);
      
      $key='System\ControlSet001\Services\El90x1\Parameters\Tcpip';
      Win32::Registry::RegOpenKeyEx
            (&HKEY_LOCAL_MACHINE,$key,NULL,&KEY_ALL_ACCESS,$RegHandle);
      Win32::Registry::RegSetValueEx
            ($RegHandle, 'IPAddress',NULL, &REG_MULTI_SZ, $finalip);
      Win32::Registry::RegCloseKey($RegHandle);

      $key='System\ControlSet003\Services\El90x1\Parameters\Tcpip';
      Win32::Registry::RegOpenKeyEx
            (&HKEY_LOCAL_MACHINE,$key,NULL,&KEY_ALL_ACCESS,$RegHandle);
      Win32::Registry::RegSetValueEx
            ($RegHandle, 'IPAddress',NULL, &REG_MULTI_SZ, $finalip);
      Win32::Registry::RegCloseKey($RegHandle);


=================================================================
.reg example
=================================================================

REGEDIT4

[HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName\ComputerName]
"ComputerName"="bobs_computer"

[HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Tcpip\Parameters]
"ComputerName"="bobs_computer"

[HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Tcpip\Parameters]
"Hostname"="bobs_computer"

[HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\El90x1\Parameters\Tcpip]
"IPAddress"="168.192.1.1"

[HKEY_LOCAL_MACHINE\System\ControlSet001\Services\El90x1\Parameters\Tcpip]
"IPAddress"="168.192.1.1"

[HKEY_LOCAL_MACHINE\System\ControlSet003\Services\El90x1\Parameters\Tcpip]
"IPAddress"="168.192.1.1"






0
 
Julian HansenCommented:
Changing the IP address ok.

But with the machine name remmber that for the name to become active you need to restart the compuater AND if the computer is a member of a domain the domain account for the computer has to be recreated in order for the machine to be able to logon to the domain.

There are API's to do this but it is fairly involved if you want to do the reboot and the creation of the domain account. If the domain account is not necessary - then much easier.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now