1

I have a batch file in my auto start which should behave a bit different on the first boot.

How can I find out if it's the first boot on the current day via Windows cmd? May be there's some way reading the Windows event log of the current day and count the boot events?

4
  • Why not use a lock file?. In the batch file see if there exists a file named with todays date. If no create the file (it is first boot). if yes it is not first boot. You could even tidy up and delete older files from prevous days.
    – DavidPostill
    Jun 28, 2015 at 12:25
  • I also thought about that but may be there's another way without creating files
    – das Keks
    Jun 28, 2015 at 12:27
  • My gut feeling says this is easier than parsing event logs (which is certainly possible). For example if you look at the event logs you have to take care of edge cases (like the system starting on a wake up timer to download updates for example).
    – DavidPostill
    Jun 28, 2015 at 12:31
  • It may help.
    – user373230
    Jun 29, 2015 at 0:41

1 Answer 1

1

This will work under the assumption that is called when the machine boots.

wevtutil is used to search the System event log for the last two Startup events (EventID=12) and from them the date is extracted. If both dates are the same, then this is not the first startup today.

@echo off
    setlocal enableextensions disabledelayedexpansion
    set "boot1="
    set "boot2="

    for /f "tokens=2 delims=T: " %%a in ('
        wevtutil qe system /count:2 /rd:true /q:"Event[System[(EventID=12)]]" /format:text 
        ^| find "Date:"
    ') do if not defined boot1 ( set "boot1=%%a" ) else ( set "boot2=%%a" )

    if "%boot1%"=="%boot2%" (
        echo This is NOT the first boot on the current day
    ) else (
        echo This IS the first boot on the current day
    )
1
  • Sorry for not reacting to you answer back then. I really like your solution. I think for Windows 10 some modifications are needed since Win10 uses the hybrid boot and just wakes from hibernate (event id 107) but generally that's what I needed.
    – das Keks
    Apr 23, 2019 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.