how to run a batch file whenever user logs in for only one time

shyam pothini
shyam pothini used Ask the Experts™
on
How to run a batch file one time for user.
that means I have a batch file to change registry keys and I want to run it when a user logs in and if the user logs in for second time it should not run.
But if another user logs in the batch file should run.
Is there any setting in registry like run or run once?

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2018
Commented:
In a domain environment, we solve this with group policy preference registry items that can be setup to run once only.
In non-domain environments, you would use either the registry key runonce or you would use the local policy (if available - it's not available on windows home editions). The local policy offers logon script deployment and you can use batches like this:
if exist %appdata%\myreg_has_been_imported goto end
reg.exe import myreg.reg && md %appdata%\myreg_has_been_imported
:end

Open in new window


This runs once only after the regimport has succeeded.
Shaun VermaakTechnical Specialist
Awarded 2017
Distinguished Expert 2018

Commented:
I would do what McKnife suggested but with Registry

@Echo Off
reg query HKCU\SOFTWARE\MySoftware /v RunOnce

IF %errorlevel% NEQ 0 GOTO Run
Goto End

:Run
MySoftware.exe
reg add HKCU\SOFTWARE\MySoftware /v RunOnce /d 1 /f

:End

Open in new window

Author

Commented:
thanks for the responses
Its domain environment but we don't want to do it with group policy
but local group policies are ok
Please guide me how to do that.
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Distinguished Expert 2018

Commented:
"We don't want to do it with group policy but local group policies are ok" - you need to tell why, because that sounds, well, crazy.

Author

Commented:
It does for me too, I like doing through group policy.
But they say its corporate policy, not to use GPO's
Distinguished Expert 2018

Commented:
But local GPO is ok?? It's the same thing, the very same thing. That makes no sense at all.
Ok, open gpedit.msc, navigate to user policy - windows settings - scripts - logon. There, link a batch.

Author

Commented:
Thanks
I kept the batch file in there.
But it run everytime user logs in. Is there any way I can run only one time for user
Distinguished Expert 2018
Commented:
As I wrote, you can use my script and it will not populate the registry more than once since it checks for the presence of that folder %appdata%\myreg_has_been_imported. If you don't want the script to run at all (why would you, it does not matter?), you can (as I wrote before) use the runonce registry key: https://msdn.microsoft.com/en-us/library/windows/desktop/aa376977%28v=vs.85%29.aspx
-> HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce

Author

Commented:
thanks

Author

Commented:
If I create reg key name application and give the path where the file is
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce

it will run everytime user logs in right?
Distinguished Expert 2018

Commented:
No, just once. MIcrosoft's article describes it:
By default, the value of a RunOnce key is deleted before the command line is run. You can prefix a RunOnce value name with an exclamation point (!) to defer deletion of the value until after the command runs. Without the exclamation point prefix, if the RunOnce operation fails the associated program will not be asked to run the next time you start the computer.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial