This post represents Step 2 of my Windows 7 Re-imaging series.
It deals with the rename computers script from a MAC address text file.
Microsoft's native tool getmac.exe was the obvious choice for local MAC address retrieval; however, the format for the MAC address presented another issue (explained later). Now, did I mention that I am not a programmer? So VBS, PowerShell, and other scripting languages are out of the question for this type of process (at least for now) even though they will provide more flexibility! So once again, Microsoft's native tools to the rescue. This time it's batch scripts. Using the getmac command in a for/f statement allows the MAC address to be variablized into the text file which extracts the computer's host name. There are a couple of ways for getting the correct syntax for the MAC address using the getmac.exe:
the getmac format for the MAC address is 00-11-22-33-44-55,
however, I use another third party tool PDQ Inventory
that obtains the MAC address format of 00:11:22:33:44:55 (I am trying to eliminate as many steps as possible).
I would have to either change all the (:) to (-) or use some sort of text replacement in my batch file.
Changing the punctuation (delimiters) is how the first batch of computers were successfully imaged and joined to the domain.
I decided it would be easier to tailor my rename computer batch script to use the format specified from my PDQ Inventory file eliminating another step and simplifying my script file and the overall reimaging process for Windows 7.
I did this; the file is called pcrenamed_from_pdq2.bat and it uses WMIC statements instead of getmac:
TITLE RENAME Computers
::This script performs the following
color B
::Delete DOD Logon banner to facilitate autologon
::ONLY NEEDED IF IMAGE HAS BEEN JOINED TO DOMAIN!*******************
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\system" /v LegalNoticeText /f
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\system" /v legalnoticecaption /f
setlocal
FOR /F "skip=1 tokens=*" %%I IN ('WMIC Path Win32_NetworkAdapter Where "NetConnectionID='Local Area Connection'" Get MACAddress') DO IF NOT DEFINED MACAddress SET MACAddress=%%I
FOR /F "usebackq delims=, tokens=1,*" %%J IN (`type C:\rename_computer\pdqmac2pcname.txt`) DO IF %MACAddress% EQU %%J SET strComputername=%%K && GOTO insert_name
goto kill_autologon
:insert_name
::sets netdomain join reg key
reg add HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce /v "netdomjoin" /t REG_SZ /d C:\rename_computer\netdom_join.bat /f
@echo on
C:\windows\system32\netdom.exe renamecomputer %COMPUTERNAME% /NewName:%strComputername% /FORCE
goto shutdown
:kill_autologon
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v AutoLogonCount /f
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v AutoAdminLogon /t REG_SZ /d 0 /f
:shutdown
shutdown -r -t 0
**Credit for this script goes to sourbread and to aryx for giving me the base for/ f statements and the MAC to name text file format
The pcrenamed_from_pdq2.bat does the following: Grabs the MAC address of the local computer and sets it as the variable of (%I)
Reads the pdqmac2pcname.txt file and matches %I (%MACAddress%) to (%J)
if %J is a match it sets and pipes %J to (%K)
%K matches so the name is inserted to the netdom statement and computer is rebooted. If name does not match, computer does NOT join domain (this way the administrator will have to manually rename and join the client to the domain. This also prevents randomly generated names for new images from being joined to the domain.
The autologoncount registry key is deleted and the autoadminlogon registry key is set to zero (to ensure autologon is disabled).
The WMIC statement provides the MAC address in the preferred format.
Ensure the pdqmac2pcname.txt text file has been edited offline (not locally in the image) and copied back to the image using Ghost Explorer.
The pdqmac2pcname.txt text file lists the MAC addresses and new computer's names for whatever computer we will reimage in this format:
00:11:22:33:44:55,NEW-PUTER-NAME
00:22:44:77:FF:AA,64A-MAINT-001
As you can see, we use another not so native tool to do the actual renaming and eventual domain join. The netdom.exe is not available in Windows 7 but can be obtained from the RSAT tools. Having the computer renamed here and rebooted seems to be the logical choice instead of just having the computer joining simultaneously when the name changes on advice from several web sites.
The computer reboots to run the netdom join domain script and...
That concludes Step 2 of this series.
Please see Step 3 for the last step of this series here, GSS, Batch Scripts and Windows 7 Reimaging Step 3.
It deals with the rename computers script from a MAC address text file.
Microsoft's native tool getmac.exe was the obvious choice for local MAC address retrieval; however, the format for the MAC address presented another issue (explained later). Now, did I mention that I am not a programmer? So VBS, PowerShell, and other scripting languages are out of the question for this type of process (at least for now) even though they will provide more flexibility! So once again, Microsoft's native tools to the rescue. This time it's batch scripts. Using the getmac command in a for/f statement allows the MAC address to be variablized into the text file which extracts the computer's host name. There are a couple of ways for getting the correct syntax for the MAC address using the getmac.exe:
the getmac format for the MAC address is 00-11-22-33-44-55,
however, I use another third party tool PDQ Inventory
![]() |
| PDQ Inventory |
I would have to either change all the (:) to (-) or use some sort of text replacement in my batch file.
Changing the punctuation (delimiters) is how the first batch of computers were successfully imaged and joined to the domain.
I decided it would be easier to tailor my rename computer batch script to use the format specified from my PDQ Inventory file eliminating another step and simplifying my script file and the overall reimaging process for Windows 7.
I did this; the file is called pcrenamed_from_pdq2.bat and it uses WMIC statements instead of getmac:
TITLE RENAME Computers
::This script performs the following
::Retrieves the MAC address using WMIC
:: Compares that MAC address to the pdqmac2pcname.txt
file for a MAC address match or mismatch.
::When the MAC address matches, the
name is piped to the netdom statement as the newname variable %strComputername%
:: then the script will goto insert_name
and
:: sets the netdom_join.bat registry key
to run upon next boot ONLY after being renamed.
:: If MAC address is found with a
computer name, the script goes to the label kill_autologon to force computer to
not autologon. This procedure prevents an improperly renamed computer from
being joined to the domain under a SysPreped randomly generated name.exit
::If name is not found or MAC Address
is not matched, the computer will not join the domain and
:: computer reboots to login screen so
that it can be manually renamed and joined to the domain.
::The client reboots.
color B
::Delete DOD Logon banner to facilitate autologon
::ONLY NEEDED IF IMAGE HAS BEEN JOINED TO DOMAIN!*******************
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\system" /v LegalNoticeText /f
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\system" /v legalnoticecaption /f
setlocal
FOR /F "skip=1 tokens=*" %%I IN ('WMIC Path Win32_NetworkAdapter Where "NetConnectionID='Local Area Connection'" Get MACAddress') DO IF NOT DEFINED MACAddress SET MACAddress=%%I
FOR /F "usebackq delims=, tokens=1,*" %%J IN (`type C:\rename_computer\pdqmac2pcname.txt`) DO IF %MACAddress% EQU %%J SET strComputername=%%K && GOTO insert_name
goto kill_autologon
:insert_name
::sets netdomain join reg key
reg add HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce /v "netdomjoin" /t REG_SZ /d C:\rename_computer\netdom_join.bat /f
@echo on
C:\windows\system32\netdom.exe renamecomputer %COMPUTERNAME% /NewName:%strComputername% /FORCE
goto shutdown
:kill_autologon
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v AutoLogonCount /f
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v AutoAdminLogon /t REG_SZ /d 0 /f
:shutdown
shutdown -r -t 0
**Credit for this script goes to sourbread and to aryx for giving me the base for/ f statements and the MAC to name text file format
The pcrenamed_from_pdq2.bat does the following: Grabs the MAC address of the local computer and sets it as the variable of (%I)
Reads the pdqmac2pcname.txt file and matches %I (%MACAddress%) to (%J)
if %J is a match it sets and pipes %J to (%K)
%K matches so the name is inserted to the netdom statement and computer is rebooted. If name does not match, computer does NOT join domain (this way the administrator will have to manually rename and join the client to the domain. This also prevents randomly generated names for new images from being joined to the domain.
The autologoncount registry key is deleted and the autoadminlogon registry key is set to zero (to ensure autologon is disabled).
The WMIC statement provides the MAC address in the preferred format.
Ensure the pdqmac2pcname.txt text file has been edited offline (not locally in the image) and copied back to the image using Ghost Explorer.
The pdqmac2pcname.txt text file lists the MAC addresses and new computer's names for whatever computer we will reimage in this format:
00:11:22:33:44:55,NEW-PUTER-NAME
00:22:44:77:FF:AA,64A-MAINT-001
As you can see, we use another not so native tool to do the actual renaming and eventual domain join. The netdom.exe is not available in Windows 7 but can be obtained from the RSAT tools. Having the computer renamed here and rebooted seems to be the logical choice instead of just having the computer joining simultaneously when the name changes on advice from several web sites.
The computer reboots to run the netdom join domain script and...
That concludes Step 2 of this series.
Please see Step 3 for the last step of this series here, GSS, Batch Scripts and Windows 7 Reimaging Step 3.

Comments
Post a Comment