PowerShell-Script to set windows date

-1

I can't program one line in PowerShell (I'm a linux user...). For a dev PC/Server with Windows on it, I need it to be always on one specific date (1.3.2017). Because it's a vmware vm the vm-supervisor sometimes resyncs the real time after freezing/snapshotting etc.

There are specific settings in the vmware supervisor, but they have no effect.

Is there a way to have it check whether the date is set on another day and set it to 1.3.2017 if neccesary?

I would want to do this every minute or so (I assume there is a cron-equivalent on windows).

philipp

Posted 2018-10-04T15:41:09.340

Reputation: 1

Answers

-1

The below will do what you are asking:

$Date_To_Set = get-date -day 01 -month 03 -year 2017
if ((Get-Date).date -ne $Date_To_Set.Date){ $Date_To_Set | Set-Date }

Task scheduler in windows is the equivalent to cron.

As I don't know your locale you may need to swap the day and month around.

EDIT: Additional information below, and have modified script with @lotpings suggestion in comments below.

It may also be worth trying this:

Run the vmtoolsd.exe timesync {enable|disable} command from the guest operating system and located at c:\Program Files\VMware\VMware Tools\vmtoolsd.

This should disable the VM itself from syncing as opposed to using the hypervisor settings.

Found from this source (https://kb.vmware.com/s/article/1318)

CraftyB

Posted 2018-10-04T15:41:09.340

Reputation: 1 488

1To remove the HHmmss part enclose the datetime in parentheses and append ().Date. --> $Date_To_Set = (Get-Date -Day 01 -Month 03 -Year 2017).Date 2nd line --> if ((Get-Date).Date -ne $Date_To_Set){ $Date_To_Set | Set-Date } The representation order of date elements doesn't matter for type datetime. – LotPings – 2018-10-04T16:13:08.437

Thank you very much! It works!

Is there any way to preserve the time? – philipp – 2018-10-05T07:01:03.950

Script has been updated to preserve the time as requested. – CraftyB – 2018-10-05T08:49:32.057