0

I want to write a simple vb script to automate shutdown in windows.

the code I am using is :

Dim ti
ti=InputBox("enter time in minutes")
ti=ti*60
Set objShell=CreateObject("WScript.Shell")
objShell.Run "cmd shutdown /s /t "& ti & " "

but when I enter the time and press enter , all I get is an command prompt window and nothing happens

I even tried by setting a default value for time and specifing the complete path for shutdown.exe ,but nothing seems to be working

  Set WshShell = WScript.CreateObject("WScript.Shell")
  Command = "C:\Windows\System32 shutdown.exe -s -t 600 "
  WshShell.Run Command

can u please correct me and guide me towards the right code ....

2
  • 1
    cmd doesn't take parameters like that. See cmd /? for usage. Commented Apr 21, 2013 at 21:16
  • when i use the full path and default value the folder C:\windows\System32 opens and nothing happens . Commented Apr 21, 2013 at 21:16

2 Answers 2

3

It looks like you're missing a backslash in your path:

  Set WshShell = WScript.CreateObject("WScript.Shell")
  Command = "C:\Windows\System32\shutdown.exe -s -t 600 "
  WshShell.Run Command
Sign up to request clarification or add additional context in comments.

1 Comment

thanks @Derek Tomes ,the above is working when full path is specified , but nothing happens when tried to execute from cmd .can you find what i am missing in there
1

If you want to run commands in cmd you have to use either /k (keep cmd window open after command finishes) or /c (close cmd window after command finishes). Here's the canonical way to do this:

ti = InputBox("enter time in minutes")
ti = ti * 60
CreateObject("WScript.Shell").Run "%COMSPEC% /c shutdown -s -t " & ti

%COMSPEC% is a system environment variable with the path to cmd.exe.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.