The situation:
You have created an unattended installation of windows (using nLite + DriverPacks BASE or other programs), but usually this isn’t enough, you need more things to be done. So we write batch files to do the following silently & automatically:
- Changing the default input language by creating & importing a .reg file
- Installing fonts
- Installing programs like kmplayer, Office (2003), and Adobe Reader
- Installing .msi (Microsoft installer) packages
- Installing Microsoft hotfixes, patches & fixes (which you didn’t embed already for whatever reason) with .exe extension
- Installing a troublesome VB6 (visual basic 6) program
- Placing a shortcut on all users’ desktop
- Getting the user input to use later (the only step which requires user interaction, obviously!)
- Changing the computer name to the name we got by user input in the previous step
- Inserting commands into batch files from another batch file
- Changing windows user’s account password based on the user input in step 8
- Disabling the automatic login behavior of windows
- Inserting executable files into startup of windows
- Restarting
- Continue running batch files after a system restart
- Configuring files to run only the first time each user logs in
- Joining the computer to a domain
During these steps we do minor stuff as well, like: creating directories, copying and deleting files, and making delays.
These batch files are originally designed for Windows XP, but most of them are usable for newer versions of windows as well (some may need minor adjustments). I don’t describe how to make the unattended installation of windows itself, or how to work with DriverPacks BASE, you can find out about them by doing a search on the web. I mention the whole design of the batch files, the clues, important notes and the examples all in one place here. So, let’s get started.
Preparation:
First, you need to consider the directory structure of the windows installation DVD (or CD). I create a folder in the root of the DVD to hold all of my extra files. Let’s name it R. so its address will be 'DVD:\R'
Second, you need to somehow pass control to your (first) batch file after windows installation. For example in nLite, in unattended section, there’s a RunOnce tab, in which you can enter commands to run after the windows installation. I just put a single command here, & that is the command to run my (first) batch file. I name my first batch file r.bat and I put it inside R directory, therefore the address is 'DVD:\R\r.bat'
nLite gives u an environment variable for DVD drive, it’s %SOURCE%
. As a result the only command I put in RunOnce tab is (remember that the variable includes a backslash, so no need to include it again):
%SOURCE%R\r.bat
You may think why not to put all the commands directly in the nLite RunOnce tab? Not doing so can have two benefits. Firstly, if you need a restart, batch files can be designed to continue running commands after the restart (unlike nLite). Secondly, if u use batch files to hold all the commands & later u need any modifications, you just change the batch files without using nLite (or going to its created files) at all.
Now that I mentioned modifications, it’s a good time to mention UltraISO software as well. You can use it to modify your DVD image properly without making it unbootable. If you just copy everything from a windows DVD (or CD) to your hard drive (& make some changes) & then write it back normally, the DVD becomes unbootable.
One of the first commands I put in my (first) batch file is the 'md'
command to create a folder on the system’s hard drive to store necessary files. Considering that the %SYSTEMDRIVE%
variable gives you the drive letter of windows partition (and unlike the %SOURCE%
variable it does not include a backslash), if windows is installed in drive C, the command below (pay attention to the backslash after the variable) creates a folder named TB in C drive’s root ('C:\TB'
):
md %SYSTEMDRIVE%\TB
If I want to copy compname.exe
(go to step 9 to find out what this file is for) from 'DVD:\R'
to this directory, I insert the following command in my batch file:
copy %SOURCE%R\compname.exe %SYSTEMDRIVE%\TB
Keep in mind that to see what a variable returns you can use echo
command followed by the variable. Example: echo %SYSTEMDRIVE%
which returns 'C:\'
if windows is installed in C drive. echo command is similar to 'cout'
or 'printf'
in C language.
The last thing I want to mention before getting to the rest of the commands themselves is a command I like to insert at the beginning of most batch files, and that is a command to make some delay, to give windows or other programs enough time to completely start up and finish their work before the batch files begin to run. It’s possible to use ping command for this. The command below will make a 60 seconds delay (for n seconds of delay you put n+1 in the command):
ping -n 61 127.0.0.1>nul
Now, for the commands themselves, we follow the list given at the beginning.
1. Changing the default input language by creating & importing a .reg file
For this step, you can add your desired languages & set the default input language on a system with windows already installed. Then go to registry editor (regedit) to 'HKEY_CURRENT_USER\Keyboard Layout\Preload'
and see what’s there. For example you see something like this:
(default) REG_SZ (value not set)
1 REG_SZ 00000409
2 REG_SZ 00000429
Then with such registry configuration, you create a .reg file containing these lines:
Windows Registry Editor Version 5.00
[HKEY_USERS\.DEFAULT\Keyboard Layout\Preload]
"1"="00000409"
"2"="00000429"
[HKEY_CURRENT_USER\Keyboard Layout\Preload]
"1"="00000409"
"2"="00000429"
Let’s call this file keyboard.reg and place it on the DVD at ‘R\keyboard.reg’. To silently import this file into registry, use this:
regedit /s %SOURCE%R\keyboard.reg
Alternatively, it’s possible to use 'reg'
command (enter 'reg /?'
or see here for more info). If you also want to change the default input language for every user that logs in (aside from the user who batch file executes in their profile), take a look at the step 16.
2. Installing fonts
I’ve placed the fonts on the DVD at ‘R\fonts’. So the command will be:
COPY %SOURCE%R\fonts\*.* %SYSTEMROOT%\fonts /y
I’ve used %SYSTEMROOT%
variable here, it holds the address of windows directory e.g. 'C:\Windows'
. The y
switch prevents the command from asking for overwrite confirmation.
3. Installing programs like kmplayer, Office (2003) and Adobe Reader
To install programs without user interaction you need to find the silent switch (plus some additional switches or parameters perhaps). You may be able to obtain it with /?
switch or you can easily figure it out by doing a search on the web. For Kmplayer, Office 2003 & Adobe Reader (11) these are the command:
%SOURCE%R\kmp_34059.exe /S /D
msiexec /i %SOURCE%R\office2003\pro11.msi /passive COMPANYNAME="company-name" PIDKEY="xxxxxxxxxxxxxxxxxxxxxxxxx"
%SOURCE%R\adobereader\setup.exe /sPB /msi /norestart ALLUSERS=1 EULA_ACCEPT=YES
The x’s in PIDKEY
in office installation command should be replaced by product key (or serial number).
4. Installing .msi (Microsoft installer) packages
msiexec.exe
is in charge of installing .msi files, below is a sample silent installation:
msiexec /i %SOURCE%SUPPORT\TOOLS\SUPTOOLS.MSI /passive
The above .msi file is located in “SUPPORT” folder in any standard Windows XP installation CD. It adds the ability to use the netdom
command which we’ll use later.
5. Installing Microsoft hotfixes, patches & fixes (which you didn’t embed already for whatever reason) with .exe extension
The following silently installs the patch that I placed in 'DVD:\R\WindowsXP-KB2286198-x86-ENU.exe'
, & saves the log at 'C:\TB\2286198.txt'
:
%SOURCE%R\WindowsXP-KB2286198-x86-ENU.exe /passive /norestart /log:%SYSTEMDRIVE%\TB\2286198.txt
6. Installing a troublesome VB6 (visual basic 6) program
Silently installing a VB6 program by itself isn’t difficult, the command is:
%SOURCE%R\program\setup.exe /s %SYSTEMDRIVE%\TB\installation.log
That installs the program & creates the log. The troublesome part happens when the setup tries to replace a windows file with an older version that comes with the installation package. This breaks the unattended installation and asks the user for overwrite confirmation. There could be multiple solutions here. The one I chose was modifying the SETUP.LST file with a text editor (I use Notepad++). If you omit the problematic files line from the SETUP.LST, the installer won’t try copying them (which are older), and as a result there’ll be no overwrite confirmation dialog box. Assume that a section of SETUP.LST looks like this:
.
.
.
File16=@imgthumb.ocx, ...
File17=@imgthumb.oca, ...
File18=@imgscan.oca, ...
File19=@imgedit.oca, ...
File20=@imgcmn.dll, ...
.
.
.
If the troublesome files are imgthumb.oca & imgedit.oca for instance, you should delete their whole lines, and renumber the remaining lines. The section after modification should be something similar to this:
.
.
.
File16=@imgthumb.ocx, ...
File17=@imgscan.oca, ...
File18=@imgcmn.dll, ...
.
.
.
7. Placing a shortcut on all users’ desktop
The file I wanted a shortcut of, was an exe file on the network. I created a shortcut of the file in windows first. A shortcut is actually a file with .lnk extension. Then I put the .lnk file to 'DVD:\R'
. The command to copy the shortcut from there to all users’ desktop:
copy %SOURCE%R\thefile.lnk "%ALLUSERSPROFILE%\Desktop" /y
The %ALLUSERSPROFILE%
variable returns 'C:\Documents and Settings\All Users'
when the operating system is Windows XP and it’s installed in drive C. Anything in this folder affects all the users rather than a specific user.
8. Getting the user input to use later (the only step which requires user interaction, obviously!)
Sometimes you may want to get user input to use later for something. Getting the user input to set computer name or a user’s password could be such a situation. To get two user inputs and save them in variables, one for the administrator password (%sn%
) and one for the computer name (%cn%
), use this:
set /P ps="Enter admin password= "
set /P cn="Enter computer name= "
9. Changing the computer name to the name we got by user input in the previous step
For this I use a 3rd party command line tool, compname.exe. To use the name we stored in %cn%
in the previous step as the computer name (and considering we already copied the compname.exe from 'DVD:\R'
to '%SYSTEMDRIVE%\TB'
), use compname.exe
like this:
%SYSTEMDRIVE%\TB\compname.exe /c %cn%
compname.exe is also capable of dynamically generating a computer name using data like MAC address, date ect. Type compname.exe /?
or see here for more info.
10. Inserting commands into batch files from another batch file
You can insert commands into batch files, or create new batch files dynamically on the fly while another batch file is running. I’ll tell you one of the reasons you may want such a thing. Remember the user inputs we got in step 8? We used one of the inputs in the next step, step 9, to set the computer name. But what if we want to use the content of %cn%
in another batch file after a system restart for instance? In this case, you don’t have access to %cn%
anymore. As a workaround, you can inject the desired command using echo
into the intended batch file while you have access to %cn%
variable.
Let’s say we want to use the command in step 9 in a batch file called second.bat
located in 'C:\TB'
after a system restart. We put the following command in the first batch file (assuming Windows is installed in drive C):
echo %SYSTEMDRIVE%\TB\compname.exe /c %cn% >> %SYSTEMDRIVE%\TB\second.bat
If you look in the 'second.bat'
file after the execution of the command above, provided that the value of 'cn'
variable is 1234, the last line should be:
C:\TB\compname.exe /c 1234
Now you have a batch file that can run on its own anytime without the need to access 'cn'
variable. Let’s break down what happened. If you open command prompt and enter the first part of the command which is:
echo %SYSTEMDRIVE%\TB\compname.exe /c %cn%
You see:
C:\TB\compname.exe /c 1234
The >>
operator is one of the command redirection operators, it can append the output to the end of a file ('second.bat'
in our case) instead of displaying it. That’s how the injection worked. Bear in mind that 'second.bat'
is located on the hard drive, so there’s no problem for modifying it (as opposed to a file on DVD/CD).
11. Changing Windows user’s account password based on the user input in step 8
For the administrator account, the next command should change the password to the input we stored in 'sn'
variable:
net user administrator %sn%
12. Disabling the automatic login behavior of windows
If you’ve set automatic administrator login for Windows (nLite can do that), you can disable this behavior so Windows asks for credentials. First, create a reg file containing these lines:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon]
"AutoAdminLogon"="0"
Then, you need to import the reg file. If the reg file is saved as 'DVD:\R\removeautologin.reg'
, make use of such a command (like step 1):
regedit /s %SOURCE%R\removeautologin.reg
If you’ve designed your batch files in a way that several system restarts happen and batch files continue running after them, you probably want to disable automatic login after you last system restart. As if you do it earlier, the automatic flow of running batch files gets interrupted for user to login.
13. Inserting executable files into startup of windows
For programs to be executed automatically when a user logs in, copy them to
%ALLUSERSPROFILE%\Start Menu\Programs\Startup
Example:
copy %SOURCE%R\sample.exe "%ALLUSERSPROFILE%\Start Menu\Programs\Startup"
Note that I’ve inserted the path inside double quotes as it contains space.
14. Restarting
The restart command is simple itself:
shutdown -r -t 30
The result of the previous command is a system restart after 30 seconds. If you want to continue running batch files after a system restart, take a look at the next step, step 15.
15. Continue running batch files after a system restart
Two notes here. Number one, obviously a batch file doesn’t start to run automatically after a system restart unless you do something for it to happen. Two, you lose %SOURCE%
variable (created by nLite) or the variables you created to store user input (like %cn%
in step 8) after a system restart (however built-in variables like %ALLUSERSPROFILE%
or %SYSTEMDRIVE%
are always available). For each of these issues, there are multiple solutions.
For the first problem, you can easily copy the batch file you want to run after a system restart to the Windows startup (pointed out in step 13) using 'copy'
command from another batch file, like this:
copy %SOURCE%R\second.bat "%USERPROFILE%\Start Menu\Programs\Startup"
The difference between this command and the one in step 13 is in their variables. Here we used %USERPROFILE%
which returns the path of current user folder (the user who the batch file is currently running in their profile). This is equal to 'C:\Documents and Settings\Administrator'
for administrator user (if Windows XP is installed in drive C).
Don’t forget that if you just leave the batch file there, it runs every time that the user logs in, which is probably not what you want. Therefore you can insert a 'delete'
command as the last command in the batch file, to delete itself when done:
del "%USERPROFILE%\Start Menu\Programs\Startup\second.bat"
This might not look like the neatest approach, but it does the job without a problem. There are alternatives however.
For the second problem, three possible resolutions are:
- Injecting commands into batch files (that are meant to run after a system restart) when you have access to needed variables (step 10). This works for any variable.
- Using a script to find DVD/CD drive letter (
Getcd.exe
is a utility for this, you can find it here. There are other scripts available on the web as well). This solves the issue of losing%SOURCE%
. - Copying the files you need from DVD/CD to a local folder on hard drive (mentioned in the preparation section). This solves the issue of losing
%SOURCE%
.
16. Configuring files to run only the first time each user logs in
Windows has a mechanism to do this called ‘active setup’. Just do a search on the web to learn about it. It works by creating a registry entry in HKEY_LOCAL_MACHINE\Software\Microsoft\Active Setup\Installed Components\
. To implement this in a batch file, you can create a .reg
file first, and then import it in Windows registry. If we want to run 'C:\TB\filetorun.exe'
only the first time each user logs in, the .reg
file should be something similar to this:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\Software\Microsoft\Active Setup\Installed Components\0]
"Version"="1"
"Stubpath"="C:\\TB\\filetorun.exe"
@="EXE file to run once only"
The double backslashes (used because the backslash is the escape character) will become single when imported to the registry. The number ‘0’ you see in the key name doesn’t mean anything special, ideally you should put a GUID (Globally Unique Identifier) there, but you don’t have to. You need to import this .reg
file in registry. As we saw earlier, regedit
can do that for you (providing we saved the .reg
file as active.reg
):
regedit /s %SOURCE%R\active.reg
17. Joining the computer to a domain
netdom
command is able to do this. netdom
is not available by default. Refer to step 4 to see how to get it. An example of this command is given here:
netdom join %COMPUTERNAME% /Domain:example.net /UserD:abcd /PasswordD:1234
The preceding command joins the computer the command is running on to the example.net domain assuming that the user ‘abcd’ with the password ‘1234’ has the right to do that. %COMPUTERNAME% returns the computer name (surprise!).
Hi,
The copy commands don’t work when I do a fresh boot. I’m running
md %SYSTEMDRIVE%\Drivers
copy %SOURCE%Drivers\driver1.exe %SYSTEMDRIVE%\Drivers
The folder “Driver” gets created but the file driver1.exe is not copied, I’ve put the same file in the “Drivers” folder in the root of the installation. Can you please help?