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.

DATE

Display the current date using the DATE command:

1DATE /T

wmic

LastBootUpTime

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

1wmic path Win32_OperatingSystem get LastBootUpTime

SystemUpTime

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

1wmic path Win32_PerfFormattedData_PerfOS_System get SystemUpTime

systeminfo

System Boot Time

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

1systeminfo | find "System Boot Time"

w32tm

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

configuration

Check the current configuration:

1w32tm /query /configuration

Register and Unregister

Register or unregister the Windows Time Service:

1w32tm /unregister
2w32tm /register

resync

Resynchronize the system clock with the configured time source:

1w32tm /resync

query source

Display the current time source and related information:

1w32tm /query /source

Win32_OperatingSystem

LastBootUpTime

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

Alternatively, execute the same command within a shell environment:

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

TIME

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

1TIME /T

tzutil

Display the current time zone:

Show the current time zone:

1tzutil /g

Change time zone

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"