r/PowerShell • • Feb 02 '23

Question Powershell script to shutdown a computer

Hi, I could use your help. I am trying to create a Powershell script that allows me to shutdown a computer after 10 minutes. Once I run the script I wanted a window to pop up telling the user that the computer would shut down in 10 minutes, and if he wanted to cancel. If he hit the cancel button the script would stop, if he didn't touch the computer after 10 minutes it would shutdown. Can you help me create a script like this? Thanks.

0 Upvotes

28 comments sorted by

View all comments

2

u/hackoofr Feb 02 '23 edited Feb 02 '23

This can also be done in VBScript, but at least show us what you have tried as code until now !

2

u/Akimotos Feb 02 '23

vbscript? <grin>

-1

u/hackoofr Feb 02 '23

Here is the VBScript that can do the trick :

 Option Explicit
 Dim Msg,objShell, intTime, intCancel
 Msg = "Your computer will shut down in 10 minutes. Press OK to continue or Cancel to stop the shutdown"
 intTime = 600
 Set objShell = CreateObject("WScript.Shell")
 intCancel = objShell.Popup(Msg, intTime, "Shutdown in Progress",vbOKCancel+vbExclamation+vbSystemModal)
 If intCancel = vbOK Then
     objShell.Run "shutdown /s /t " & intTime, 0, True
 End If

3

u/Akimotos Feb 02 '23

I would never go back using VBScript.
But it's a solution to a problem.