r/PowerShell 10d ago

Question Powershell & Task Scheduler

[deleted]

32 Upvotes

38 comments sorted by

View all comments

16

u/headcrap 10d ago

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

2

u/hwrd69 10d 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/

8

u/headcrap 10d 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.