VBS Script – Count Number of times computer has turned on

The following scripts can be used to count the number of times a computer has been turned on.

It queries the Windows event log for Event 12 which records ‘The operating system started at system time [x]’ each time Windows loads.

It then counts the number of Event 12’s and echos the number.

The second script can be used on remote computers as long as you have Administrator rights on the machine.

To use the scripts you will need to copy into a text document and save the file is a .vbs extension.

You can also download a copy below.

 

 Count number of times computer has turned on:

Output:

VBS-CountStarted2

VBS Code:

count = 0
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colLoggedEvents = objWMIService.ExecQuery _
("Select * from Win32_NTLogEvent Where Logfile = 'System'" _
& " and EventCode = '12'")

For Each objEvent in colLoggedEvents
count = count + 1

Next

wscript.echo "Number of times operating system has started:   " & count

 

Remotely count number of times computer has turned on:

Output:

VBS-CountStarted1

VBS-CountStarted2

VBS Code:

count = 0
strComputer=InputBox ("Enter the network name for the remote computer")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colLoggedEvents = objWMIService.ExecQuery _
("Select * from Win32_NTLogEvent Where Logfile = 'System'" _
& " and EventCode = '12'")

For Each objEvent in colLoggedEvents
count = count + 1

Next

wscript.echo "Number of times operating system has started:   " & count

Article Downloads

TIP: You may need to right-click and select 'save link as'.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *