Mastering Time with Shell and PowerShell

Overview

Explore an array of shell and PowerShell commands focused on time management, including last boot-up time, system boot time, and time zone adjustments.

Display the current date using the DATE command:

1DATE /T
Shell

Retrieve the last boot-up time using Windows Management Instrumentation Command-line (wmic):

1wmic path Win32_OperatingSystem get LastBootUpTime
Shell

Determine system uptime, showing the duration since the last boot-up:

1wmic path Win32_PerfFormattedData_PerfOS_System get SystemUpTime
Shell

Obtain the system boot time using the systeminfo command in conjunction with find:

1systeminfo | find "System Boot Time"
Shell

The w32tm command-line tool is used for diagnosing and configuring the Windows Time Service.

Check the current configuration:

1w32tm /query /configuration
Shell

Register or unregister the Windows Time Service:

1w32tm /unregister
2w32tm /register
Shell

Resynchronize the system clock with the configured time source:

1w32tm /resync
Shell

Display the current time source and related information:

1w32tm /query /source
Shell

Calculate the time elapsed since the last boot-up in PowerShell by subtracting the LastBootUpTime from the current date:

1(get-date) - (gcim Win32_OperatingSystem).LastBootUpTime
PowerShell

Alternatively, execute the same command within a shell environment:

1powershell.exe -c "(get-date) - (gcim Win32_OperatingSystem).LastBootUpTime"
Shell

Obtain the current system time using the TIME command with the /T flag:

1TIME /T
Shell

Show the current time zone:

1tzutil /g
Shell

Change the system time zone using the tzutil command by providing the desired time zone as an argument after the /s flag. For example, set the time zone to Eastern Standard Time:

1tzutil /s "Eastern Standard Time"
Shell