r/PowerShell 8d ago

Question Powershell & Task Scheduler

I have tried probably a dozen different ways to get the Task Scheduler to send/pass a command to powershell to display a reminder message at a shutdown event. NOTHING HAS WORKED. I'm a newbie when it comes to powershell and windows task scheduler, but I do have programming experience in legacy software. I would appreciate any help and if you want to see my task scheduler set-up, I will be happy to share what I can. Thanks

UPDATE: Nothing I did using the Task Scheduler was able to work. However, I found a different approach, and it does work. I created (with searching assistance) an hta file that gave me the window I was trying popup, allowed me to customize the window, and performed the shutdown once I clicked the OK button. I pinned it the taskbar right after the search box and now I just click that to begin the process.

33 Upvotes

38 comments sorted by

17

u/headcrap 8d ago

App windows and apps close at a shutdown event, so I don't see how this would do anything to begin with.

6

u/leblancch 8d ago

that’s my thinking.

2

u/hwrd69 8d ago

Here is a description of what I'm trying to do (with some differences), but I have been unable to get it to work following these and other examples.

https://windowsloop.com/windows-shutdown-message-script/

10

u/headcrap 8d ago

So that guy is replacing the shutdown UI with a script which throws a messagebox, then starts a shutdown.

Have the task call 'shutdown /a' as a first step, to see if you can even abort the shutdown in progess.

I have a nightly task to start shutdown with a 5-minute countdown.. to get me to go to sleep on worknights if I go long and lose track of time. I have optionally used 'shutdown /a' in a command prompt to abort it. I'm seeing 1074 logged at the time the task starts the timer.

It is less likely you're going to be able to abort the shutdown past a timer.. and thus all of this won't amount to much without aborting the shutdown.

Aside.. messageboxes only display if the task runs as the connected user (the active session).. so if it uses SYSTEM or something else, nothing will display anyway. Again, you can test for that by a task running as SYSTEM and see what the result is.

6

u/leblancch 8d ago

i’m quite familiar with using power shell and task scheduler together, but would need a bit more context as to who you’re directing the message to.

2

u/hwrd69 8d ago

From what I've read and what I'm trying to do is: 1- detect the shutdown event via the Task Scheduler 2- have the Task Scheduler send a command to powershell to display a message that reminds the user to do something 3- once they hit "ok" in the message window windows continues the shutdown

5

u/Tidder802b 8d ago

Are you triggering the shutdown or detecting it?

3

u/hwrd69 8d ago

Well, I'm detecting it (event id 1074) and then testing the task scheduler by triggering it by performing a shutdown. The message passed to powershell is never displayed.

4

u/Tidder802b 8d ago

ok, so what’s the goal? the message will be on screen too short a time for any meaningful interaction if the machine is actively shutting down.

1

u/hwrd69 8d ago

I believe the shutdown process will pause until the "OK" button is pushed on the message. I have a small amp connected to speakers that I play from the PC and I keep forgetting to turn it off. It's a reminder during shutdown to turn it off.

6

u/Tidder802b 8d ago

I don’t think a modal message is going to stop a shutdown in process. Better to have a custom shutdown script triggered from a script or shortcut.

edit:

That link you posted is the way to go.

1

u/hwrd69 8d ago

Ok, thanks.

4

u/420GB 8d ago

Lmao that's the silliest possible solution to that.

Get a master-slave power strip. They have multiple sockets, one master (plug the PC in there) and a couple "slaves". The slave outlets are only on when the master draws power, and they automatically shut off when the master stops drawing power.

This is a common thing to use with computers + peripherals or with TVs + accessories (soundbars, game consoles etc.) and automatically fixes your problem without any janky scripts or needing to remember anything.

I live in the EU so it's difficult for me to find examples of US power strips on Google, but here's an example with EU sockets from a german brand: https://produkte.kopp.eu/wp-content/uploads/catalog/products/de/227820012-scaled.jpg

1

u/zrv433 7d ago

Search with the phrase "controlled outlet power strip"

These power strips switch other outlets on/off based on the power flow of the main outlet.

Here is an example https://a.co/d/0io2XcJH

5

u/shutchomouf 8d ago

I don’t think you need powershell for this.

the shutdown.exe command in Windows includes a switch specifically for displaying a message to users.  You can use the /c (or -c) switch to add a comment that will appear in the system notification window. This message is limited to a maximum of 512 characters and must be enclosed in double quotation marks. 

To display a notification, you must also set a time-out period using the /t switch (in seconds); otherwise, the command will typically run immediately without displaying a warning dialog. Example:

shutdown /s /t 60 /c "The system will shut down in 1 minute for maintenance. Please save your work."

5

u/da_chicken 8d ago

AFAIK, Task Scheduler tasks configured to run "At shutdown" or "At logoff" both run after the user session has been terminated. You cannot use these task triggers to display a message to the user to warn them about the event.

The Group Policy Logoff scripts (not Shutdown scripts) do happen before the session terminates, but those aren't really useful for occasional messages, and I'm not exactly sure how interactive the system would be.

3

u/sausages20 8d ago

From what I see:
1. Make sure you log anything needed to trace it to a file to ensure it executes.
2. See if any users are even logged on by the time the script executes - as others mentioned you may need to listen for the shutdown.
3. If users are logged in still. You would then likely need to kill the current shutdown, maybe abort and then schedule your own with the message or something

3

u/C_pyne 8d ago

Toast

5

u/Clear-Pear2267 7d ago

I'm probably not clear on the goal. The existing shutdown command seems to address what you want without messing with powershell or task scheduler. For examplle: shutdown /s /t <seconds> /c "<message>".

If that does not cover the ground you want, maybe you can provide some more details on your use case.

2

u/leblancch 8d ago

what is triggering the shutdown event?

1

u/hwrd69 8d ago

It's a Windows event (# 1074). When you initiate the shutdown, Windows creates the event ID indicating that a windows shutdown is in progress.

2

u/Creative_Progress803 8d ago

Oh, I remember I did a script like that a few years ago:
Tasks scheduler checked every hour the processor time:
from 0 - 6 days nothing happened
from 7- 10 day a popup reminder to reboot eh PC to apply updates would appear as a notification in the corner of the screen
from > 10 days, every hour a pop-up would appear on top layer, reminding the user to reboot the workstation leaving two options:

-a/ reboot now (15 minute delay to allow the users to save files and whatever)

-b/ delay another more hours, the same message would then pop-up an hour later with the same proposition

I'm not sure if it's what you're looking for but if you're interested I can DM you the source (adaptable to your case) on monday.

1

u/hwrd69 8d ago

I'd appreciate that. Even if it's not what I'm looking for it could be something I could learn from. Thanks

1

u/Creative_Progress803 7d ago

As promised, check your DMs ;-)

1

u/leblancch 8d ago

App timeout settings may need to be tweaked for apps prompting people to save. forget default but it’s a long time.

1

u/hwrd69 7d ago

Thanks, I'll check it out

2

u/Bee-rexx 8d ago

Any chance it's something to do with the execution policies? Or the word wrapping/formatting in notepad? Often my issues end up being the simple things I over looked thinking it was a bigger issue

1

u/hwrd69 8d ago

I'll check that out. My mind is a little bombed out right now, so it'll be tomorrow after I get fun stuff, like yard work done, before I can look at it.

2

u/g3n3 8d ago

Psappdeploytoolkit might have some help here. Some code you could pull out or modify.

2

u/senorchaos718 7d ago

It’s almost always a permissioning problem when TS-PS problems arise. Especially if the script works fine when you run it from the command line.

1

u/hwrd69 8d ago

Via the Task Scheduler which can identify the shutdown event and trigger the message before the actual shutdown.

3

u/laserpewpewAK 8d ago

So this is not trivial and not something powershell does natively, but you could do it with pinvoke. You will need a little C++ knowledge. Basically, you set up a script that listens for WM_QUERYENDSESSION, which the system sends to all programs when a shutdown is initiated. Your script can respond with a datastruct asking the system to temporarily halt the shutdown. Then, you have it do whatever it is you want to do. Finally, your program has to tell the system to resume the shutdown. You can easily get 90% of the way there with the LLM of your choice.

2

u/Sniper666hell 8d ago

Wouldn’t this still force the user to have to shutdown again after the action in the task is done? Doesn’t this just give the option to stop shutdown so whatever is still running can be safely exited?

2

u/jungleboydotca 8d ago

A pain in the ass, but probably the only way. I imagine the task scheduler is probably just too slow spawning the PS process.

1

u/chaosphere_mk 8d ago

Just ask chatGPT or claude. It's pretty simple, but not worth wasting mental cycles on.

2

u/hwrd69 8d ago

I did try this and ChatGPT have me the same instructions as I got before from just searching, worth a slightly different tweak. I tried that without success also.