Say you dual boot Windows and Linux, and use the simple and light-weight systemd-boot to switch OS. Say you boot to Linux by default. However, this causes headaches when Windows updates and needs to restart as part of the process, especially if it has to do so multiple times (for example major feature updates).
You could change the default right before you boot
Create a script %windir%\System32\update\run\precommit.cmd
with the following contents:
powershell.exe "C:\Scripts\set-windows-next-reboot.ps1"
This precommit.cmd script will be run by Windows prior to the feature update being applied and the system rebooting..
Install the powershell module uefi2
. Create the second script, "C:\Scripts\set-windows-next-reboot.ps1" with the following contents:
Import-Module UEFIv2 -ErrorAction Stop
$guid="{4a67b082-0a4c-41cf-b6c7-440b29bb8c4f}" # namespace for LoaderEntryDefault, LoaderEntryOneShot
# Bytes spell out the value "a u t o - w i n d o w s".
[byte[]] $bytes = 97,0,117,0,116,0,111,0,45,0,119,0,105,0,110,0,100,0,111,0,119,0,115,0,0,0
Set-UEFIVariable -namespace $guid -VariableName "LoaderEntryOneShot" -ByteArray $bytes
# Set-UEFIVariable -namespace $guid -VariableName "LoaderEntryOneShot" -Value "a u t o - w i n d o w s"
This sets a UEFI variable, LoaderEntryOneShot
, that tells systemd-boot to boot Windows on the next update.
This method does not check whether the update in question requires a restart, but will set the next boot to Windows regardless. The Microsoft documentation is unclear whether the script runs only on updates with restarts, nor whether it runs for each restart within an update.