One place at which I worked we manufactured CRT terminals. One of the circuits watched for a drop in the power supply voltage. When a drop in voltage was detected a signal called Power good” changes state, and the state told other parts of the system to do something. That’s all I remember about that signal. The rest was taken care of by Martin Eberhard.
Yes, it’s possible (I have used it) but it’s been many years since I have done so. And I don’t know if my experience holds for Windows 11.
However, it may help you to solve your problem.
I believe this article addresses your issue. It presents one way with some programming. But the second suggestion I’d the one I’m familiar with. Use Group Policy Editor to add a Group Policy Shutdown script. I’d do some research in this area.
Perhaps other Windows users are very familiar with this technique. I hope they join in.
Security Policies in Windows allow for intervention at boot, login, logout, and shutdown. Since you can specifies programs or scripts to run in association, it is the easiest way I can think of to do what you want.
Keep in mind that if your script is going to be taking over the shutdown or reboot sequence, you need to make sure it can cleanly handle exceptions, and deals with expected conditions or you may find yourself with a very hard to deal with locking up rather than a backing up.