This really isn't a PowerShell question. However, you can use a group policy to run a script on shutdown -- with one constraint: you have to disable the "fastboot" feature for the script to run reliably.
How to run a batch file when windows 11 when shutdown is activated
I need to activate a USB relay using serial code on shutdown
2 answers
Sort by: Most helpful
-
-
Hania Lian 19,831 Reputation points Microsoft Vendor
2024-12-18T06:07:57.67+00:00 Hello,
To run a batch file when Windows 11 shuts down, you can use the Task Scheduler to create a task that triggers on shutdown. Here are the steps:
Open Task Scheduler:
Press Win + S, type "Task Scheduler", and press Enter.
Create a New Task:
In the Task Scheduler, click on "Create Task" in the right-hand Actions pane.
General Settings:
Give your task a name and description.
Select "Run whether user is logged on or not".
Check "Run with highest privileges".
Triggers:
Go to the "Triggers" tab and click "New".
In the "Begin the task" dropdown, select "On an event".
Set the "Log" to "System", "Source" to "User32", and "Event ID" to "1074" (this event ID corresponds to system shutdown).
Actions:
Go to the "Actions" tab and click "New".
In the "Action" dropdown, select "Start a program".
In the "Program/script" field, browse to your batch file or type its path.
Conditions and Settings:
You can adjust conditions and settings as needed, but the defaults should work for most cases.
Save the Task:
Click "OK" and enter your password if prompted.
This setup will ensure your batch file runs whenever the system is shutting down.Best Regards,
Hania Lian
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.