Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This batch is used with a few other files as start-up scripts as a part of the Symantec Ghost Cast re-imaging process for Windows 7 we are about to undertake. Together they totally automate the entire re-imaging process of Sysprep, renaming the computer from text file, then joining the computer to the domain and placing in the correct OU.

I know there are other programming languages and scripting languages that can perform these same processes far better but no time for learning them at this moment. Sure MSs Power-shell and VBS are better candidates but I am not spent up on those just quite yet, nor are my coworkers. Other renaming applications works intermittently for me. Compname.exe does not work with Windows 7. Wsname works intermittently or not at all with our some of Dell models 790, Power Edge servers 850, 860, and R200. Our other models do not seem to have any major issues using the third party applications--GX620, 740, 745, 755, 760, and 780.

I prefer to use native applications already within the OS. Obviously netdom.exe is not native (have to obtain files from the RSAT executable) but it is an easy OS import and a must have on the images.

So please, provide any answers with the programs I am currently using (batch, netdom, getmac, and good ole text files).

This batch file accomplishes the following:

  1. Uses getmac.exe to find the MAC address of the local machine (being reimaged)
  2. Variablizes the local MAC address (%%G)
  3. Pipes the variablized MAC address and compares it to getmac2.txt file (%%A)
  4. Grabs the associated variable (%%B) and
  5. Places the %%B as the strComputername
  6. The && statement stops the operation when the MAC address is found instead of parsing the entire file
  7. The :insert_name label runs netdom.exe, closes the batch and reboots the computer. The /FORCE switch is used but not listed here while testing. The 'pause' is used only for testing as well.

A subsequent script joins the computer to the domain.

Now, this is what I would like to try... I would like to be able to modify the %%G variable by replacing the dashes '-' in the MAC address with colons ':'. Getmac.exe produces the MAC address like this 11-22-33-44-55-66. However, another admin program provides me a list with MAC addresses like this 11:22:33:44:55:66. My getmac2.text file lists the MAC addresses with the colons. Obviously, I can automatically remove the colons from a text editor but the intent is to reduce as many steps as possible. Also piping the %%G variable to another file would not be a problem if it is easier to perform the string replacement.


getmac2.txt file contents are formatted like this:

11:22:33:44:55:66,88D-350-03-780 11:22:33:44:55:77,44E-351-13-790 11:22:33:44:55:88,66F-352-23-760 11:22:33:44:55:99,22G-353-33-R200 11:22:33:44:55:00,11H-354-43-620

setlocal enabledelayedexpansion

FOR /F "usebackq delims= " %%G IN (`getmac.exe /FO table /NH`) DO (
    FOR /F "usebackq delims=, tokens=1,2" %%A IN (`type C:\wsname\getmac2.txt`) DO if %%G EQU %%A SET strComputername=%%B && goto insert_name

C:\windows\system32\netdom.exe renamecomputer %COMPUTERNAME% /NewName:%strComputername%

share|improve this question
up vote 1 down vote accepted

Use %variable:from=to% or !variable:from=to! (with delayed expansion).

set mac=%%G
set mac=%mac:-=:%
share|improve this answer
Hi grawity, I am not sure how to write the syntax for the suggestions you added. Where would I add those lines? – Terrence Mar 23 '13 at 3:44
I got where to place the set statements from another site. I put them everywhere except in the DO statement. – Terrence Mar 23 '13 at 22:51

You must log in to answer this question.

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