Add a Custom Script to Windows Setup
Windows Setup scripts:
ErrorHandler.cmd and Setupcomplete.cmd are custom scripts that run during or after the Windows Setup process. By using cscript/wscript scripts, they can be used to install applications or run other tasks.
%WINDIR%\Setup\Scripts\SetupComplete.cmd:
After the user sees the desktop, this script runs immediately. We can disable this setting by using OEM product keys. This runs with local system permission.
%WINDIR%\Setup\Scripts\ErrorHandler.cmd:
Automatically, this script runs when Setup encounters a fatal error and runs with local system permission.
Windows Unattend scripts:
Create an Unattend.xml file with one of these settings to run during the Windows Setup process and can be used with OEM product keys.
Use RunAsynchronousCommands to run services or commands that can start at the same time. Use RunSynchronousCommands to run commands that need to finish before other commands that start.
Microsoft-Window-Shell-Setup\LogonCommands\AsynchronousCommand works such as LogonCommands\AsynchronousCommand: The commands using these unattended settings are now started at the same time, and no longer wait for the previous command to finish.
LogonCommands\Asynchronous
Some of these settings run in the user context. Others run in the system context that depends on the configuration pass. Windows Setup starts as RunSynchronous Command runs a script or Add Microsoft-Windows-Setup\RunAsynchronousCommand and is helpful for setting hard disk partitions.
Add RunSynchronousCommand or Microsoft-Windows-Deployment\RunAsynchronousCommand to the audit User configuration pass to run a script that runs when the PC enters audit mode and can be helpful for tasks such as automated app installation or testing.
Add Microsoft-Windows-Shell-Setup\LogonCommands\AsynchronousCommand
FirstLogonCommands\SynchronousCommand to run after the Out of Box Experience (OOBE). On Concord, before the user sees the desktop. Especially this can be useful to set up language-specific apps or content after the user has already selected their language.
Use these scripts sparingly because long scripts can prevent the user from reaching the Start screen quickly. For retail versions of Windows, additional restrictions apply to these scripts. For info, see the Licensing and Policy guidance on the OEM Partner Center.
It will be triggered on the next boot when you add a script using FirstLogonCommands. Even if you boot into audit mode using Ctrl+Shift+F3. Add the setting to boot to audit mode without triggering these scripts. Microsoft Windows-Deployment\Reseal\Mode = Audit.
Run a script after setup is complete (SetupComplete.cmd)
Order of operations
Windows Setup searches for the SetupComplete.cmd file in the %WINDIR%\Setup\Scripts\directory after Windows is installed but before the log on screen appears.
Windows Setup runs the script if a SetupComplete.cmd file is found. Windows Setup logs the action in the C:\Windows\Panther\UnattendGC\Setupact.log file.
Setup doesn’t verify any error levels or exit codes in the script after it executes SetupComplete.cmd.
Warning You cannot reboot the system and resume running SetupComplete.cmd. By adding a command such as shutdown-r as you should not reboot the system. This will put the system in a bad state.
The Group Policy is defined in the domain is not applied to the computer until Setupcomplete.cmd is finished if the computer joins a domain during installation. This is to confirm that the Group Policy configuration activity does not interfere with the script.
Run a script if Windows Setup encounters a fatal error (ErrorHandler.cmd)
When you are installing many systems at the same time then this script is useful. This detects when an error occurs during Windows Setup. Automatically, Setup runs a script that can contain custom actions or commands when it does to address the cause of the error.
Windows Setup searches for a command script in the following directory if Windows Setup encounters a fatal error and is prevented from completing the installation: %WINDIR%\Setup\Scripts\ErrorHandler.cmd. Depending on whether the script is found one of two actions will occur.
A dialog box is displayed with the error text if the script is not found. A user must dismiss the dialog box before Windows Setup exits.
The script executes synchronously if the script is found. No error text or dialog box is displayed. Windows Setup exits after the ErrorHandler.cmd script has finished running.
For instance, the computer will return to the environment from which Windows Setup was executed such as an earlier version of the Windows Preinstallation Environment or operating system (Windows PE) depending on the phase of Windows Setup.
There may be instances when Windows Setup encounters more than one error. This runs the ErrorHandler.cmd script more than once. Confirm that you can run this script multiple times when developing the code for ErrorHandler.cmd.
You can do either of the following to use ErrorHandler.cmd:
Add it to the image, in %WINDIR%\Setup\Scripts\ErrorHandler.cmd and Mount the image and unmount the image.
or
Add ErrorHandler.cmd to a temporary file location, for instance, C:\Temp\ErrorHandler.cmd and then run Windows Setup using the /m option.
You May Also Like: How to Setup guest account on Windows 10