1

I'm making a batch script to check on local networked PCs and would like the boot time or uptime of each PC set to a variable so I can display data however I like.

I use this command to print to screen:

SystemInfo /s PC_NAME | find "Boot Time:"

and get this result:

System Boot Time:          27/09/2019, 9:15:16 AM

But I'd like just the "27/09/2019, 9:15:16 AM" part set as a variable.

Thanks

S.Hampton
  • 55
  • 1
  • 6
  • Possible duplicate of [How to format date/time output in a Windows batch script?](https://stackoverflow.com/questions/55983714/how-to-format-date-time-output-in-a-windows-batch-script) – Compo Sep 27 '19 at 09:13

2 Answers2

1

Be aware that systeminfo is localised. So "Boot Time" is only true for english versions of Windows.

So it would be better using WMIC for any system language like this code below :

@echo off
Title Last Boot Time
for /f %%a in ('WMIC OS GET lastbootuptime ^| find "."') DO set "DTS=%%a"
set "BOOTTIME=%DTS:~0,4%-%DTS:~4,2%-%DTS:~6,2%  %DTS:~8,2%:%DTS:~10,2%"
echo DTS      : %DTS%
echo BOOTTIME : %BOOTTIME%
pause

And if you like with a vbscript like that :

Option Explicit
' Declare variables
Dim colItems, objItem, objWMIService,ws,Title
Dim strBoot, strBootDate, strBootDay, strBootHour, strBootMins
Dim strBootMonth, strBootTime, strBootYear, strComputer, strMsg, strQuery
Title = "La dernière heure de démarrage de votre ordinateur by Hackoo 2018"
Set ws = CreateObject( "WScript.Shell" )
strComputer = ws.ExpandEnvironmentStrings( "%COMPUTERNAME%" )

If IsEmpty(strComputer) Then
    WScript.quit()
ElseIf  strComputer = "" Then
    strComputer = "."
End If

' Connect to specified computer
Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
Set colItems = objWMIService.ExecQuery( "Select * from Win32_OperatingSystem", , 48 )
For Each objItem in colItems
    strBootYear  = Left( objItem.LastBootUpTime, 4 )
    strBootMonth = Mid( objItem.LastBootUpTime,  5, 2 )
    strBootDay   = Mid( objItem.LastBootUpTime,  7, 2 )
    strBootDate  = DateValue( strBootDay & "-" & strBootMonth & "-" & strBootYear )
    strBootHour  = Mid( objItem.LastBootUpTime,  9, 2 )
    strBootMins  = Mid( objItem.LastBootUpTime, 11, 2 )
    strBootTime  = strBootHour & ":" & strBootMins
    strBoot = strBootDate & ", " & strBootTime
    strMsg  = "La dernière heure de démarrage du PC : " & chr(34) & strComputer & chr(34) & " est : " &_
    vbCrlf & strBoot
Next

' Display results
MsgBox strMsg,vbInformation,Title

'Done
WScript.Quit(0)
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • According to [this answer](https://stackoverflow.com/a/54259310/1161484) the `LastBootupTime` from WMI was "unstable with time synchronization and hibernation and unusable in practice." I've confirmed this through testing. Retrieving the startup event from the event log is robust to localization (you're only searching for the event code) and possible to do via command line with `evtviewer`. – Daniel Widdis Sep 28 '19 at 20:24
  • I like your batch script but where would I slip in the hostname/s of the local networked PCs? – S.Hampton Oct 09 '19 at 02:15
0

Figured it out although I'm sure there would be a more efficient way:

@ECHO Off
SET PC_NAME=BillyPC
SET TEMP_FILE=C:\temp\temp.txt
SystemInfo /s %PC_NAME% | FIND "Boot Time:" > %TEMP_FILE%
SET /p BOOT_TIME=<%TEMP_FILE%
SET BOOT_TIME=%BOOT_TIME:~27%
ECHO %BOOT_TIME%
PAUSE

Cheers

S.Hampton
  • 55
  • 1
  • 6