Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I know that there is a simple way to shutdown the computer in command prompt, but is it possible to do system updates and then shutdown the computer in command prompt?

For example, when Vista needs to install updates, it will let me install them and shut down by clicking the Shut Down button (while relegating the normal shut down button into the menu). So is there a command to do this in command prompt?

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

There is no inbuilt command-line way to force install of waiting updates before shutdown/reboot (I've heard rumor that it's in Windows 8, but haven't checked).

You're best bet is to create a script (VBS, PowerScript, or alike) which will perform the updates and then shut the system down.

For example:

' NAME : <wusforceupdate>
' AUTHOR : <Samuel lambert>
' COMMENT : <Find-Download-Install-reboot (with windows update system WUS)>
' www.wsus.info/forums/lofiversion/index.php?t5454.html

'Shutdown flags;
 const nLog_Off          =  0 
 const nForced_Log_Off   =  4  '( 0 + 4 ) 
 const nShutdown         =  1
 const nForced_Shutdown  =  5  '( 1 + 4 ) 
 const nReboot           =  2
 const nForced_Reboot    =  6  '( 2 + 4 )
 const nPower_Off        =  8
 const nForced_Power_Off = 12  '( 8 + 4 )

ShutdownOption = nForced_Reboot

'Monthly log;
dt = date() : nMonth = Year(dt)*1e2 + Month(dt)
sLogFile = "C:\WUSforceupdate-" & nMonth & ".log"


Set updateSession = CreateObject("Microsoft.Update.Session")
Set updateSearcher = updateSession.CreateupdateSearcher()

Set searchResult = _
updateSearcher.Search("IsInstalled=0 and Type='Software'")

Set File = CreateObject("Scripting.FileSystemObject")
Set LogFile = File.OpenTextFile(sLogFile, 8, True)

LogFile.WriteLine("***************************************************************")
LogFile.WriteLine( "START TIME : " & now)
LogFile.WriteLine( "Searching for updates..." & vbCRLF)
LogFile.WriteLine( "List of applicable items on the machine:")

For I = 0 To searchResult.Updates.Count-1
  Set update = searchResult.Updates.Item(I)
  LogFile.WriteLine( I + 1 & "> " & update.Title)
Next

Set WshShell = WScript.CreateObject("WScript.Shell")

If searchResult.Updates.Count = 0 Then
  LogFile.WriteLine( "There are no applicable updates.")
  WshShell.popup "There are no applicable updates.",1
  ShutDown(ShutdownOption)  '<-- O P T I O N A L
  Wscript.quit
End If

LogFile.WriteLine( vbCRLF & "Creating collection of updates to download:")

Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")

For I = 0 to searchResult.Updates.Count-1
  Set update = searchResult.Updates.Item(I)
  LogFile.WriteLine( I + 1 & "> adding: " & update.Title )
  updatesToDownload.Add(update)
Next

LogFile.WriteLine( vbCRLF & "Downloading updates...")

Set downloader = updateSession.CreateUpdateDownloader() 
downloader.Updates = updatesToDownload
downloader.Download()

LogFile.WriteLine( vbCRLF & "List of downloaded updates:")

For I = 0 To searchResult.Updates.Count-1
  Set update = searchResult.Updates.Item(I)
  If update.IsDownloaded Then
    LogFile.WriteLine( I + 1 & "> " & update.Title )
  End If
Next

Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")

LogFile.WriteLine( vbCRLF & _
   "Creating collection of downloaded updates to install:" )

For I = 0 To searchResult.Updates.Count-1
  set update = searchResult.Updates.Item(I)
  If update.IsDownloaded = true Then
    'WshShell.popup "installing..." & vbNewLine & update.Title,1
    LogFile.WriteLine( I + 1 & "> adding: " & update.Title )
    updatesToInstall.Add(update) 
  End If
Next

logFile.WriteLine( "Installing updates...")
Set installer = updateSession.CreateUpdateInstaller()
installer.Updates = updatesToInstall
Set installationResult = installer.Install()

'Output results of install
LogFile.WriteLine( "Installation Result: " & installationResult.ResultCode )
LogFile.WriteLine( "Reboot Required: " & installationResult.RebootRequired & vbCRLF )

LogFile.WriteLine( "Listing of updates installed " _
    & "and individual installation results:" )

For I = 0 to updatesToInstall.Count - 1
  LogFile.WriteLine( I + 1 & "> " & updatesToInstall.Item(i).Title _ 
    & ": " & installationResult.GetUpdateResult(i).ResultCode ) 
Next

If installationResult.RebootRequired = -1 then
  LogFile.WriteLine( "RebootRequired")
  ' <-- normally now you should call for a R E B O O T.....
End if

ShutDown(ShutdownOption)  '<-- O P T I O N A L

LogFile.WriteLine( "STOP TIME : " & now)
LogFile.WriteLine("***************************************************************")
LogFile.Close

wscript.echo "Updates are installed"


Function ShutDown(sFlag)
 wscript.sleep 600
  Set OScoll = GetObject( _
     "winmgmts:{(Shutdown)}").ExecQuery( _
     "Select * from Win32_OperatingSystem") 
  For Each osObj in OScoll
    osObj.Win32Shutdown(sFlag)
  Next
End Function
share|improve this answer
    
wow all that code lol! Ill try it though, thanks for the help. –  mendez Oct 29 '11 at 11:55
    
@mendez Yeah, it's a pretty thorough example. :) –  techie007 Oct 29 '11 at 16:01
add comment
  • WuInstall 1.2 or higher is able to do what you want. But the website says that once your time-limited trial expires, you have to buy a license. Licenses are only sold in big multi-packs: the smallest multi-pack is USD$250.

    I use WuInstall 1.1, which is freeware. It can install updates and reboot, but it can't install updates and shut down.

    I've written more about WuInstall elsewhere.

  • Just curious: Why do you want to install updates and shut down without clicking the mouse? If you have a lab of many identical computers, consider using WSUS and/or other software. Or maybe you could scrape by with macro-playback software.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.