Running a batch file on logoff

8

4

I have a batch file that makes a directory on H:\ and copies all of my CS work from I:\ to H:\ ( our I:\ drive isn't always available ). Is there a way to possibly run this batch file on logoff so that my files are always updated, WITHOUT using gpedit.msc (access denied)?

I am on a student computer, so most likely installing software won't to practical, but I could try. And help would be much appreciated =)

Or is there a way to run a batch file, and not have the commands run until like.. 8:50 before the bell rings, so I could just put it in the Startup folder and it could run at that time?

I thought about it some more, and the easiest way would be to set a scheduled task at 8:50 for it to run.. but it would still be nice if I knew how to to do in a batch file. I don't have access to the shutdown -l commands, and I don't believe that would help anyways because I want the script to run when I click 'Log Off'.

cutrightjm

Posted 2011-10-11T12:30:58.020

Reputation: 3 966

Answers

1

I eventually just used Scheduled Tasks and set the batch file to run at 8:50, that should work.

cutrightjm

Posted 2011-10-11T12:30:58.020

Reputation: 3 966

5

This should do the trick. Save as a REG file and import, unless that is blocked as well. This effectively creates a logoff script entry.

[HKEY_CURRENT_USER\Software\Policies\Microsoft\Windows\System\Scripts\Logoff\0\0]
"Script"="H:\your-script-here.bat"
"Parameters"=""
"ExecTime"=hex(b):00,00,00,00,00,00,00,00,00,00,00 ,00,00,00,00,00

Save your script to do the copy under the root of your H: drive.

Tim Brigham

Posted 2011-10-11T12:30:58.020

Reputation: 1 102

1Can I use it if my O.S (Windows 7 Home Premium) doesn't have a pre-installed gpedit.msc? – user3133076 – 2014-12-23T09:52:57.700

4

Another similar option to what Tim Brigham already suggested is to save the text below as a .reg file:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Logoff\0]
"GPO-ID"="LocalGPO"
"SOM-ID"="Local"
"FileSysPath"="C:\Windows\System32\GroupPolicy\User"
"DisplayName"="Local Group Policy"
"GPOName"="Local Group Policy"
"PSScriptOrder"=dword:00000001

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Group Policy\Scripts\Logoff\0\0]
"Script"="H:\yourScriptFileName.bat"
"Parameters"=""
"IsPowershell"=dword:00000000
"ExecTime"=hex(b):00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00

This is similar to doing it yourself from the Group Policy Editor tool (gpedit.msc).

Oz Edri

Posted 2011-10-11T12:30:58.020

Reputation: 259

1Welcome to Super User! You've got a great answer for a new user, better than a lot of others I've seen. If you need help, be sure to follow the [tour] and check out the [help]. We hope you enjoy participating on [se]! – bwDraco – 2014-12-06T03:14:43.257

@Oz, (or Tim-Brigham), adding a logoff script via a REG IMPORT cmd doesn't seem to work in Windows 10 (Build 10240), even though a REG QUERY shows the exact same data as when I create the logoff script via gpedit.msc. I performed a reboot, but no difference. Any ideas? – atreyu – 2017-08-15T14:37:33.703

1

Ah...nm. Solved it myself using Regshot to track all changes to the registry before and after using gpedit.msc, and also, crucially, to %WINDIR%\System32\GroupPolicy\User\Scripts\scripts.ini.

– atreyu – 2017-08-15T16:03:10.837

2

you can put that line at the end of your batch file

shutdown -l

this will log you off as soon as the script is done

cwin

Posted 2011-10-11T12:30:58.020

Reputation: 121

I know this is a late comment, but I didn't use this command because A) I don't have the access rights and b) I wouldn't want another user in the next class period to use my computer if it were to take longer than usual – cutrightjm – 2012-03-25T03:53:47.560

1

I'm not sure which version of Windows added this program, but you could use the waitfor command to pause a batch file for 3,000 seconds (i.e. 50 minutes), then continue with your copy command.

It's definitely available in Windows 7, almost certainly in Vista, and not in XP. However, it may be available in the Windows 2003 Resource Kit Utilities that are available to download, which will run on XP.

There's probably some batch trickery that's possible to pause a script for a particular amount of time too.

afrazier

Posted 2011-10-11T12:30:58.020

Reputation: 21 316

-1

Start -> Run and type "gpedit.msc"

Under User Configuration -> Windows Settings -> Scripts (Logon/Logoff) you can add your scripts

Layticia

Posted 2011-10-11T12:30:58.020

Reputation: 458

I upvoted this answer just to make sure that it stays here as a reference. I know it's weird to give an answer which the OP (specifically) doesn't want. But it's good that it's here IMO. – Shashank Sawant – 2014-10-10T21:46:49.203

The OP explicitly stated that access to gpedit.msc was denied. – afrazier – 2011-10-11T13:37:43.760