0

I'm trying to create a batch file to shutdown my computer from the taskbar on my Surface Pro 3, which I have done successfully on my desktop computer (Both running Windows 8.1), but on my Surface this batch file:

@echo off
SET /p var1 = "Shutdown? (Y/N) "
if /I %var1% == "Y" shutdown /s /t 0

results in an error "Shutdown unexpected at this time." alongside an immediate crash of the command window containing it. This exact (Edit: wording poorly chosen, not actually exact) batch file works as intended on my desktop. A batch file on my Surface simply containing the code:

shutdown /s /t 0

works fine, which is very puzzling to me. I haven't found any solutions from my searches, so any help would be greatly appreciated!

Ryan
  • 3
  • 2
  • That code works on your desktop? It shouldn't; you're missing quotes around `%var%` in your `if` statement and your `set` command shouldn't have spaces around the `=`. – SomethingDark Aug 12 '15 at 04:27
  • Also, [`choice`](http://www.robvanderwoude.com/choice.php) would work better than `set /p` in this situation. – UnknownOctopus Aug 12 '15 at 04:29
  • The quotes and spaces fixed it, and I rechecked my desktop's version and it was correct to begin with. I shouldn't have called it the exact same batch file, as I didn't copy/paste from my desktop to my surface. Thanks for the help SomethingDark – Ryan Aug 12 '15 at 05:14

2 Answers2

0
@echo off
SET /p var1="Shutdown? (Y/N) "
if /I "%var1%" == "Y" shutdown /s /t 0

This version works, all that needed fixing was spaces around the = and quotes around %var1. Thanks to SomethingDark for this answer.

Ryan
  • 3
  • 2
0

Why not use Powershell's Stop-Computer cmdlet? The cmdlet is available in Powershell 4.0.

-Edit to add an example. Thanks to this question for the Yes/No confirmation code, confirmed working in Powershell v4.

$message  = 'Shutdown Computer?'
$question = 'Are you sure you want to Shutdown?'

$choices = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
$choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes'))
$choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&No'))

$decision = $Host.UI.PromptForChoice($message, $question, $choices, 1)
 if ($decision -eq 0) {
  Stop-Computer -force
} else {
 "Shutdown aborted."
}
Community
  • 1
  • 1
user4317867
  • 2,397
  • 4
  • 31
  • 57