@echo off & setlocal enableextensions
call :GetDateTimePS start_
echo Whatever ...
pause
call :GetDateTimePS endat_
call :TimeDiffPS "%start_%" "%endat_%" elapsed_
echo Start at "%start_%"
echo Ended at "%endat_%"
echo %%elapsed_%%=%elapsed_%
endlocal & goto :EOF
:: The indentation is essential to avoid incorrect syntax
:GetDateTimePS
setlocal
for /f "usebackq delims=" %%A in (`powershell -NoLogo -NoProfile -Command ^
"Get-Date -Format 'dd.MM.yyyy HH:mm:ss'"`) do (
set "result_=%%A")
endlocal & set "%1=%result_%" & goto :EOF
:TimeDiffPS
setlocal
for /f "usebackq tokens=1-3 delims=" %%a in (`
powershell -NoProfile -Command ^
"$a=[datetime]::ParseExact('%~1','dd.MM.yyyy HH:mm:ss',$null);"
"$b=[datetime]::ParseExact('%~2','dd.MM.yyyy HH:mm:ss',$null);"
"($b-$a).ToString()"`) do (set result_=%%a)
endlocal & set "%3=%result_%" & goto :EOF
The output might be e.g.
Whatever ...
Press any key to continue . .
Start at "09.08.2026 19:08:00"
Ended at "09.08.2026 19:20:36"
%elapsed_%=00:12:36