Windows 7 Forums
Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.

Windows 7: Run a program when user enters his account

21 Apr 2017   #1

Windows 7 Ultimate x64
Run a program when user enters his account

I have a batch file which I want to run when user enters his account.
Not just at user logon, but every time he enters his account (when switching accounts).
I know there is a group policy "run these programs at user logon", but it is not suit my case.

My System SpecsSystem Spec
21 Apr 2017   #2

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan


There’s an event that fires each time a user returns to their login session. To achieve your goal, what you can do is create a scheduled task based on this event.

Event id ‘4778’ of provider ‘Microsoft-Windows-Security-Auditing’ in the ‘Security’ log is your answer. It is disabled by default and needs to be activated first however. Enable the “Audit logon events” policy under “Computer Configuration\Windows Settings\Security Settings\Local Policies\Audit Policy” to have your computer log the event. After this, an event log will be created whenever a user logs in or switch users into an account.

You can now open up Task Scheduler and create a scheduled task from this event. Here’s an example you can import and modify to your use.
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="">
    <Description>Performs a task when a user reconnects to their login session.</Description>
    <URI>\User login event</URI>
      <Subscription>&lt;QueryList&gt;&lt;Query Id="0" Path="Security"&gt;&lt;Select Path="Security"&gt;*[System[Provider[@Name='Microsoft-Windows-Security-Auditing'] and EventID=4778]]&lt;/Select&gt;&lt;/Query&gt;&lt;/QueryList&gt;</Subscription>
    <Principal id="Author">
  <Actions Context="Author">
      <Arguments>/c "C:\Users\Pyprohly\Desktop\Ic\file.bat"</Arguments>
The event will trigger on any connection to a user account session and as so often will the batch file execute. If you want your batch file to target a specific user, you must amend your batch file to test for it.

The event message contains information on which account had their session reconnected, but reading this in from batch can be tricky. Instead, what you can do is query the physically logged on user (as opposed to the account in which the batch file runs when you expand “%username%”) through WMI.
@echo off

for /f "tokens=2 delims=\" %%I in (' wmic computersystem get username /value ') do for /f "delims=" %%J in ("%%~I") do (
	if "%%~J" neq "Pyprohly" exit /b

rem Continue batch program here
My System SpecsSystem Spec
21 Apr 2017   #3

Windows 7 Ultimate x64

It seems to work :)
Thank you!
My System SpecsSystem Spec


 Run a program when user enters his account

Thread Tools

Similar help and support threads
Thread Forum
Virus that enters Windows user account password automatically
Hi, I have Windows 7 Home premium OS on my gateway laptop which has only one user account and on turning on laptop that user password gets entered Automatically which is unexpected and its very long. Then i have to hit enter which shows Wrong password message and then it allows me to enter my...
System Security
Program not in User account when installed from within Administrator.
When you install a program from within Administrator account should it automatically be available from within the User account as well? I installed a program from within the Administrator account but when I go the the User account it is no where to be found. Is this program specific?
Program Won't Start in One User Account But Will In The Other
Hello, I'm having a problem and I'm not sure what to do. I installed a program to my main user account which is an administrator account. When I click on the program to open, the mouse pointer changes like the program is going to load and then it stops. I logged into my second user account which is...
Standard User Account Program Access
Hello All, I have set up a standard user account on my Son's laptop, but there are software programs I would like him to have access to, without me having to log on with Administrator ever time. When he try's to run said program it says Parental Controls has blocked this program the...
Performance & Maintenance
Any way to reassign an existing program to a new user account?
I've got a large program that I've installed on my wife's laptop, that I'd like to be available only when I log onto her computer. She currently is the only USER. I installed the program (it takes many discs to install, plus it has been customized post-installation) when logged in as her, and...
General Discussion

Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd

All times are GMT -5. The time now is 20:26.
Twitter Facebook