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

4

u/raysfandan Feb 02 '23
# Pop up window to confirm shutdown
$ConfirmBox = [System.Windows.MessageBox]::Show("Your  computer will shut down in 10 minutes. Do you want to cancel the shutdown?", "Shutdown Confirmation",  [System.Windows.MessageBoxButton]::YesNo)

# If user clicked "No", proceed with shutdown
if ($ConfirmBox -eq "No") {
  Start-Sleep -Seconds 600
  Stop-Computer
}

3

u/SMFCAU Feb 02 '23

Need to start the timer outside of your if statement, otherwise it doesn't catch the prompt being ignored completely (EG - computer unattented).

-10

u/MrInkognitus Feb 02 '23

This code doesn’t work. Is identical to mine code, but de counter only start when I click on cancel button, but I need the countdown start immediately when the program start, and the program stop if I click on cancel button.

13

u/BlackV Feb 02 '23

how would we know, you didn't post your code.....