How to run a batch file when windows 11 when shutdown is activated

richard norling 0 Reputation points
2024-12-17T09:40:07.49+00:00

I need to activate a USB relay using serial code on shutdown

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,584 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,481 Reputation points
    2024-12-17T19:56:20.1333333+00:00

    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.

    0 comments No comments

  2. 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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.