How to Set Date and Time using Powershell’s “Set-Date” module

How to Set Date and Time using Powershell’s “Set-Date” module

The common way to manually Set/Adjust Date and/or Time is to go to “Date and Time Settings“, then “Adjust Date/Time“, and make sure that you have NOT turned on “Set Time automatically“toggle. If you have, turn off the “Set Time automatically” toggle as well as “Set Time zone automatically” then proceed to set them accordingly. This is the GUI way. There is a way to achieve the same in CLI via Powershell in Microsoft Windows. What makes commands in CLI work is the power of Powershell modules.

 

For all the commands below, you need to have a Windows Machine and run Powershell (preferably as Administrator to avoid issues with permissions)!

To successfully set the DATE/TIME to the desired date/time on your Windows machine, below is the syntax [in Powershell];

Set-Date -Date <DateTime>

NOTE:// “Set-Date” is used to set to both “Date” (day,month,year) as well as “Time”. “<DateTime>” in the command above represents the Date/Time you intend to change to.

 

[Setting the DATE]

Option A

First, check what the date is:

Get-Date

Next, Set the Date:

Set-Date -Date "29/02/2020"

Next, verify that the Date changed;

Get-Date

 

Option B

If the Date did not change, use the following alternative (its a fairly hard, but interesting way);

Set-Date -Date(Get-Date).AddDays(-240)

NOTE:// “240” in the command above is assumed to be the difference between Today’s (now) date and the Current date (as it reads in your machine).

To get that difference, we can set 2 variables (‘X‘ and ‘Y‘) to hold the values of our intermediate computations, and a third variable (‘Z‘) to hold the final computation.

This first computation has its output as the “Day of Year”, of Today’s date. To understand what Day of Year means, remember that there are 365/366 days in a year. “January 1” is day one of the year, “February 1” is day 32 of the year, and so on. For the following command, let’s assume that the actual date now (Today) is July 14, 2021. To get the Day of Year and save to variable ‘X‘;

$X = (Get-Date -Year 2021 -Month 7 -Day 14).DayOfYear

 

This second computation has its output as the number of days remaining till the end of Year 2020, from the date that is reading on the Windows machine (February 29, 2020). For this, we need to get the Day of Year with a command similar to the first command above, then subtract that from “366” (2020 is a Leap Year). Below is the command saved to variable ‘Y‘;

$Y = 366 - ((Get-Date -Year 2020 -Month 2 -Day 29).DayOfYear)

This third computation has its output as the addition of ‘X’ and ‘Y’ obtained above;

$Z = $X + $Y

 

All that is remaining now is to run the command to set the date with an “AddDays” parameter that takes “Z” obtained above as input;

Set-Date -Date(Get-Date).AddDays(-Z)

 

[Setting the TIME]

To set time, just calculate the hours you need to add/subtract to the current time, and then use the command;

Set-Date -Adjust -0:45:0

where the first ‘0‘ represents 0 hours, “45” represents 45 Minutes, and the last ‘0‘ represents 0 Seconds.

 

To verify that the Time has been successfully set, verify using the command;

Get-Date

 

How to Set Date and Time using Powershell’s “Set-Date” module
Powershell | thetqweb