Exit/Close/Kill Powershell process on OS shutdown


I'm working on a powershell script that use System.IO.FileSystemWatcher class.
I run the script in background (1 powershell process is create with the -noexit option)
BUT when the workstation initiate a reboot/shutdown, the powershell process (like a cmd process) don't close (exit) automatically !!! :((( until the user choose to terminate the process !
(i m using windows XP)

is there a solution to kill my powershell process ?? or something else

Tkx in advance.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kilz3mAuthor Commented:
or is there another way to launch powershell script in background ?
I tryed invoke-command but WSMAN:\localhost\shell\idletimeout (or $pssessionoption.idletimeout = 0), on both side, didn t work (le session closed and didn t stay alive).
kilz3mAuthor Commented:
well in fact, let me explain :
I need to monitor change on filesystem (file in a directory) and trigger an action if change occur (i m using the .NET System.IO.FileSystemWatcher class)
i run my script (launch by a vbs script in order to hide the powershell window and i use the the NoExit option in order to keep my powershell process open) (1 powershell process is created and stay open), my script create 4 Register-ObjectEvent and the idle until System.IO.FileSystemWatcher event occur.

If i dont use the NoExit option my script execute and close and finaly my Register-ObjectEvent disappear.

My script work like a charm BUTT :
When a user want to shutdown or restart is computer, the powershell process dont automatically close !! until the user choose to terminate the process ! (on windows XP)

is there a solution to AUTOMATICALLY kill my powershell process ON SHUTDOWN/RESTART event ?? or something else ?
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

x-menIT super heroCommented:
gpedit.msc - Computer Compiguration - Windows Settings -Scripts (Shutdown)

Place a "taskkill.exe /im powershell.exe" batch or script in it
x-menIT super heroCommented:
or maybe:
Name: Taskill.exe
Parameters: /IM powershell.exe

knock yourself out ;)
kilz3mAuthor Commented:
User and Computer GPO script execute only after every user process have been killed ... ://
My powershell process block the gpo script execution !
x-menIT super heroCommented:
how about a Logoff script?
kilz3mAuthor Commented:
a logoff script is a script that configured by GPO (User Setting) or Localy with gpedit.msc (User Configuration).
And is executed when every user process are closed and logoff process continu to execute script in group policy setting.

In my case, powershell process block the logoff process and the script is never executed !
x-menIT super heroCommented:
how about changing the logof behavior on the registry, forcing the shutdown of running applications

check this:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kilz3mAuthor Commented:
thank you x-men
i only found this solution too, but it s a litle violent ;)

Set-ItemProperty 'hkcu:\control panel\desktop' AutoEndTasks 1

I think that there is no other solution for Windows XP
kilz3mAuthor Commented:
a litle violent ;)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.