Fixing Laptop Time Issues: Solving Clock Problems with Dead CMOS and Battery

Edi Naic - Jun 12 - - Dev Community

Step 1: Create a PowerShell Script

  1. Open Notepad or any text editor.
  2. Copy and paste the following PowerShell script into the text editor:
# PowerShell script to sync system time with an internet time server
Try {
    Write-Output "Updating system time from time.windows.com..."

    # Start the Windows Time service if it is not running
    $service = Get-Service -Name w32time
    If ($service.Status -ne 'Running') {
        Start-Service -Name w32time
        Start-Sleep -Seconds 5  # Give the service time to start
    }

    # Update time using W32tm (Windows Time Service)
    w32tm /config /manualpeerlist:"time.windows.com" /syncfromflags:manual /reliable:YES /update
    Start-Sleep -Seconds 5  # Wait for a few seconds to ensure the configuration is updated
    w32tm /resync
    Write-Output "System time updated successfully."
} Catch {
    Write-Output "Failed to update system time."
    $_.Exception.Message
}

Enter fullscreen mode Exit fullscreen mode

Save the file with a .ps1 extension, for example, UpdateTime.ps1.

Step 2: Create a Scheduled Task

  • Open Task Scheduler: Press Windows + R, type taskschd.msc, and press Enter.
  • Create a New Task: In the Task Scheduler, click Create Task in the Actions pane on the right.
  • General Tab:

Name your task, e.g., "Update System Time on Startup".
Choose "Run whether the user is logged on or not".
Check "Run with highest privileges".

  • Triggers Tab:
    Click New to create a new trigger.
    From the Begin the task dropdown menu, select "At startup".
    Click OK to save the trigger.

  • Actions Tab:
    Click _New _to create a new action.

From the Action dropdown menu, select "Start a program".

In the Program/script box, type powershell.exe.

In the Add arguments (optional) box, type the following (replace the path with the path to your script):

powershell
-File "C:\autoTimebyEd\UpdateTime.ps1"
Click OK to save the action.

  • Conditions Tab:
    Uncheck "Start the task only if the computer is on AC power" if you want the task to run on battery power as well.

  • Settings Tab:
    Check "Allow task to be run on demand".
    Optionally, you can adjust other settings as needed.

  • Save the Task:
    Click OK to save the task.
    You may be prompted to enter your user account password.

  • Testing the Task
    Run the Task Manually from the Task Scheduler:

In Task Scheduler, locate the task you created.
Right-click the task and select "Run".
Check if the system time updates correctly.

Restart Your Laptop:

  • Restart your laptop to ensure the script runs at startup and updates the time. Following these steps, your laptop should automatically fetch and update the system time from time.windows.com each time it powers on. This setup ensures your system clock is always accurate without requiring manual intervention.
.