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
holden321

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
Pyprohly

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

Hi,

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.
Code:
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2017-04-21T21:42:09.7887974</Date>
    <Author>DESKTOP-K9PKB3G\Pyprohly</Author>
    <Description>Performs a task when a user reconnects to their login session.</Description>
    <URI>\User login event</URI>
  </RegistrationInfo>
  <Triggers>
    <EventTrigger>
      <Enabled>true</Enabled>
      <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>
    </EventTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>S-1-5-21-3458303600-101233123-2075927670-1001</UserId>
      <LogonType>InteractiveToken</LogonType>
      <RunLevel>LeastPrivilege</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>cmd</Command>
      <Arguments>/c "C:\Users\Pyprohly\Desktop\Ic\file.bat"</Arguments>
    </Exec>
  </Actions>
</Task>
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.
Code:
@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
holden321

Windows 7 Ultimate x64
 
 

It seems to work
Thank you!
My System SpecsSystem Spec
.

Reply

 Run a program when user enters his account




Thread Tools




Similar help and support threads
Thread Forum
Possible to copy all Desktop/Program settings to another user account?
So yesterday I found that aero had become disabled. To check whether it was a windows problem, or a graphics card problem, I created another user account and logged into that and aero still worked there confirming that it was a windows user account problem. The fix here worked just fine. ...
General Discussion
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?
Software
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...
Software
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 19:19.
Twitter Facebook Google+ Seven Forums iOS App Seven Forums Android App