Thursday, November 25, 2010

VB Script to run gpupdate.exe /force silently without a reboot

If you use Gpupdate.exe /force in your logon scripts ypu would've of found by now that sometimes if not most often when running gpupdate.exe you are asked to either logoff or reboot to apply configuration settings. You've also probably found that there is no exact command like 'Gpupdate.exe /Force /Silent.'

The below VBscript below will run Gpupdate.exe /force without any user interaction it will also not show any log off or reboot requests.

To implement this in a logon script place the code below into its own SilentGPupdate.vbs file. Then call it from your logon script, I've set the WshShell.Run not to wait to finish each command because gpupdate.exe can run in its own space and time (doing this will keep your login times quick).

===============
'VB Script for Refreshing GroupPolicy Settings silently
'Script Version: 0.2
'Created: 25/11/2010
'Created By: Joejoeinc.com

'Define Variables and Objects.
Set WshShell = CreateObject("Wscript.Shell")

'Note: Gpupdate command has to be run twice as the ECHO command can't answer more than one question.

'Refresh the USER policies and also answer no to logoff if asked.
Result = WshShell.Run("cmd /c echo n | gpupdate /target:user /force",0,true)

'Refresh the Computer policies and answer no to reboot.
Result = WshShell.Run("cmd /c echo n | gpupdate /target:computer /force",0,true)

'Hand back the errorlevel
Wscript.Quit(Result)
===============

Props to Craig for pointing out that if you name the script gpupdate.vbs you will cause a loop.

11 comments:

  1. No Problem. Thank you for the feedback.

    ReplyDelete
  2. The script is not working for me. When I run it, it infinitly starts the script over and over, had 1800 processes of it running, and it maxes out the PC until I delete the actual script to get it to stop.

    ReplyDelete
  3. Are you sure you don't have a loop elsewhere in your code? There is no looping in my code at all. It simply executes one line and then the next line.

    I've tested the script on Win7 and XP and works fine.

    ReplyDelete
  4. Hello! The loop you are experiencing is because the command is calling gpupdate...

    Result = WshShell.Run("cmd /c echo n | gpupdate /target:user /force",0,true)

    The post says name your file GPUpdate.vbs.. so instead of calling GPUPDATE.exe the script just calls itself, looping & eventually crashing your PC.

    This should only have an effect if you execute the script when testing on your local machine.

    Renaming the file to something other than GPUpdate.vbs solved it for me.

    I wouldn't have noticed it if the guy next to me hadn't of helped out.

    Thanks

    P.S.

    Great Post @JoeJoeinc

    ReplyDelete
  5. Thanks Craig. I did not notice I had did that. thank you for the feedback. Great pick up.

    ReplyDelete
  6. Nice work Joe!!! This is exactly what I have been hunting for... for a very long time. Cheers :-)

    Although instead of having:
    Wscript.Quit(Result)

    I have:
    Result = WshShell.Run ("cmd /c echo n | gpupdate /force",0,true)
    ObjLogFile.WriteLine "GPUpdate Result: " & Result
    Wscript.Quit

    I like logging :-)

    Thanks Again!!

    ReplyDelete
  7. This comment has been removed by a blog administrator.

    ReplyDelete
  8. You can also simply do the following:

    (echo n & echo n) | gpupdate force

    ReplyDelete
  9. gpupdate.exe /target:user /force /wait:0 does the same job:

    Dim WshShell
    Set WshShell = CreateObject("Wscript.Shell")
    WshShell.Run "gpupdate.exe /target:user /force /wait:0",0,true
    Set WshShell = Nothing

    ReplyDelete
  10. ThanX works perfect

    ReplyDelete