We help IT Professionals succeed at work.

scripting

Hi,
I’m running a windows 7 pc and I would like to run a gpupdate / force script.  I used a text file and typed in gpupdate /force and saved it as GP.bat.  When I double click the on the bat file it just keeps running the gpupdate /force command continuously and rapidly.  How do I get it to run once and exit the command promt?
Comment
Watch Question

Commented:
you can write a C# console program with process and set wait on exit on that process then terminate the program after process is completed.
BRONZE EXPERT

Commented:
You probably did something wrong. I've tried this and runs command once and then exits.
Steve KnightIT Consultancy
SILVER EXPERT

Commented:
You called the batch file gpupdate.cmd perhaps?  That would loop back and run itself in a loop...
IT Consultancy
SILVER EXPERT
Commented:
Open Notepad.
Type in
@echo off
gpupdate /force
pause


Save As
Type in Rungp.cmd (or something other than gpupdate.cmd)
Make sure it says "all files" not "text files" as otherwise it can sometimes add .txt on the end

The @echo off before gpupdate stops the gpupdate command being shown on the screen, just the results
And pause afterwards if you wish to see any complaints it has before stopping.

Steve
Bill PrewTest your restores, not your backups...
SILVER EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Another thing you can do at a command prompt in Windows 7 is type:

where gpupdate

and it should show you all the possible items it would run.  If you do see a BAT or CMD file in the list then as Steve mentioned, you may have a case where the script is calling itself, rather than the EXE.

~bp

Commented:
Just a wild guess from a sleepy guy! Have you tried to run your batch file as Administrator?
"Right click" on "GP.bat", Run As Administrator.
Bill PrewTest your restores, not your backups...
SILVER EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
And in the interest of thoroughness, take a look at the Windows Event logs, in the "System" log and see if there are any messages from GroupPolicy in there.

~bp

Author

Commented:
Dude, you are good!
I did call it gpupdate.bat
So the command can't be the name of the file.
THANK YOU!!!
Steve KnightIT Consultancy
SILVER EXPERT

Commented:
No problem, gets us all sometimes... calls itself then in an infinite loop :-)

Commented:
searched all over google for this answer and you were right - it's because I called it gpupdate.bat as well. Can someone help me better understand why calling the first part of the name causes an infinite loop? regardless if it's .bat or .cmd?
Steve KnightIT Consultancy
SILVER EXPERT

Commented:
If you type or run from within a batch file just the command "gpupdate" then it looks in the current directory for (I think the order is)

gpupdate.com
gpupdate.exe
gpupdate.bat
gpupdate.cmd
gpupdate.vbs
etc. + various others until it finds one, and it not in the current dir then it looks down the path until it gets to c:\windows\system32 and finds gpupdate.exe

So.... call it gpupdate.bat / .cmd and if in that batch file you use gpupdate then it runs itself again as it finds itself before the real gpupdate.exe

You could use "%windir%\system32\gpupdate.exe" within a file called gpupdate.bat to specifically get that exe, or do

@echo off
cd /d "%windir%\system32"
gpupdate.exe /force

But easier... don't call it gpupdate.bat

Steve

Commented:
Thanks Steve. I completely understand now. Thanks!