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

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?

shyam pothiniAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Open in new window

This runs once only after the regimport has succeeded.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Shaun VermaakTechnical SpecialistCommented:
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

reg add HKCU\SOFTWARE\MySoftware /v RunOnce /d 1 /f


Open in new window

shyam pothiniAuthor 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.
 Acronis Global Cyber Summit 2019 in Miami

The Acronis Global Cyber Summit 2019 will be held at the Fontainebleau Miami Beach Resort on October 13–16, 2019, and it promises to be the must-attend event for IT infrastructure managers, CIOs, service providers, value-added resellers, ISVs, and developers.

"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.
shyam pothiniAuthor Commented:
It does for me too, I like doing through group policy.
But they say its corporate policy, not to use GPO's
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.
shyam pothiniAuthor Commented:
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
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:
-> HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
shyam pothiniAuthor Commented:
shyam pothiniAuthor Commented:
If I create reg key name application and give the path where the file is

it will run everytime user logs in right?
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows 10

From novice to tech pro — start learning today.