Unattended Installation of Windows Server 2022 on VirtualBox

This post describes the unattended installation of a Windows Server 2022 Virtual Machine in Oracle VirtualBox.

General Preparations for new VM Installations

These steps have to be done once and can be used to install also other guest Operationg Systems (e.g. Linux):
Add the path of vboxmanage to the Windows Path environment variable. For example with Win+r and then enter:

rundll32 sysdm.cpl,EditEnvironmentVariables

Add C:\Program Files\Oracle\VirtualBox to the User variable Path.
A directory c:\vms for the virtual machines on the host system should exist:

mkdir c:\vms

Preparation of the Windows Server 2022 installation

Download the Windows Server 2022 iso and place it in the folder c:\sw (e.g. c:\sw\Windows_Server_2022_x64.iso).

Install the new Windows Server 2022 VM

Configure the Virtual Machine

Run these steps in a Windows command prompt to configure the new VM. You probably need to adjust the parameter –bridgeadapter1 to the name of your primary interface of the Windows host system (ipconfig /all => see Description)

set "VHOST=" && set "MAC="
(set /p VHOST=Enter VM name ^(e.g. win1^):
set /p MAC=Enter the MAC address of the VM if you would like to use a dedicated MAC address. Press Enter to let VirtualBox assign a random MAC: 
call vboxmanage createvm --name %VHOST% --ostype Windows2022_64 --register
call vboxmanage modifyvm %VHOST% --clipboard-mode=bidirectional --drag-and-drop=bidirectional --audio-controller=hda --audio-out=on
call vboxmanage modifyvm %VHOST% --memory=8192 --cpus=4 --vram=128 --graphicscontroller=vboxsvga --accelerate-3d=on --usb-xhci=on
call vboxmanage setextradata %VHOST% GUI/ScaleFactor 2
call vboxmanage storagectl %VHOST% --name "IDE" --add ide
call vboxmanage storagectl %VHOST% --name "SATA" --add sata --bootable on
call vboxmanage createmedium disk --filename c:\vms\%VHOST%\%VHOST%_1.vdi --size 122880 --variant Standard
call vboxmanage storageattach %VHOST% --storagectl "SATA" --port 0 --device 0 --type hdd --medium c:\vms\%VHOST%\%VHOST%_1.vdi
call vboxmanage modifyvm %VHOST% --nic1 bridged --bridgeadapter1 "Intel(R) Ethernet Connection (2) I219-LM"
if defined MAC call vboxmanage modifyvm %VHOST% --mac-address1=%MAC% 
call vboxmanage sharedfolder add %VHOST% --name=sw --hostpath=c:\sw --automount --auto-mount-point=s:\sw
call vboxmanage showvminfo %VHOST%|findstr /C:"NIC 1")

Perform the unattended OS installation

The next command will display all installation images contained in the iso file. Select the on you wish to install and enter the Product Key:

set "img=" && set "pkey="
(vboxmanage unattended detect --iso=c:\sw\Windows_Server_2022_x64.iso
set /p img=Enter the Image ^# you want to install: 
set /p pkey=Enter the Windows Product Key: )

The output should be similar to:

VBoxManage.exe: info: Detected 'C:\sw\Windows_Server_2022_x64.iso' to be:
    OS TypeId    = Windows2022_64
    OS Version   = 10.0.20348.350
    OS Flavor    = ServerStandard
    OS Languages = en-US
    OS Hints     =
    Image #1     = Windows Server 2022 Standard (10.0.20348.350 / x64 / en-US)
    Image #2     = Windows Server 2022 Standard (Desktop Experience) (10.0.20348.350 / x64 / en-US)
    Image #3     = Windows Server 2022 Datacenter (10.0.20348.350 / x64 / en-US)
    Image #4     = Windows Server 2022 Datacenter (Desktop Experience) (10.0.20348.350 / x64 / en-US)
    Unattended installation supported = yes
Enter the Image # you want to install: 4
Enter the Windows Product Key: xxx

Now start the unattended install. This will take about 10 minutes:

vboxmanage unattended install %VHOST% --iso=c:\sw\Windows_Server_2022_x64.iso --image-index=%img% --key=%pkey% --user=user1 --user-password=changeme --install-additions --additions-iso="C:\Program Files\Oracle\VirtualBox\VBoxGuestAdditions.iso" --locale=de_DE --country=DE --time-zone="Europe/Berlin" --hostname=%VHOST%.fritz.box --start-vm=gui
rem

After a reboot your new Windows 2022 VM is now ready to use. Have fun!
After the OS is installed you might perform the following:

rem to be run on the host system
rem disable monitor timeout
vboxmanage guestcontrol %VHOST% run --username=Administrator --password=changeme --wait-stdout --exe "C:\Windows\System32\powercfg.exe" -- /x monitor-timeout-ac 0
rem change keyboard layout to German
vboxmanage guestcontrol %VHOST% run --username=user1 --password=changeme --wait-stdout --exe "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -- Set-WinUserLanguageList -LanguageList  de-DE, en-US -force

Also we can set the Network Profile of the Ethernet Adapter to Private.

Further information