Vysakh Venugopal
asked on
Powershell showing old time after changing the time zone
Here's my powershell script:
What I am trying to achieve is that 1. Get current system time 2. Compare it with the Universal Time (utc-6) 3. If same -> nothing to be done. If not -> change system time zone to utc-6
Everything works, but once after setting the system time zone to utc-6, when I checks the current system time, it shows me the old time itself (though I can see that the system time zone has changed)
Looks like something like caching. Is there anything like that? Or Is there anything wrong with my script?
Can someone please lead me through a good way?
$time = (Get-Date).ToShortTimeString()
Write-Host "Current Time: "$time
$destzone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Central Standard Time")
$desttime = [System.TimeZoneInfo]::ConvertTimeFromUtc((Get-Date).ToUniversalTime(), $destzone)
$desttime = $desttime.ToShortTimeString()
Write-Host "UTC-6 = "$desttime
$newtime = (Get-Date).ToShortTimeString()
Write-Host "Current Time: "$newtime
if($newtime -eq $desttime)
{
Write-Host "ok"
}else
{
C:\Windows\System32\tzutil.exe /s "Central Standard Time"
}
What I am trying to achieve is that 1. Get current system time 2. Compare it with the Universal Time (utc-6) 3. If same -> nothing to be done. If not -> change system time zone to utc-6
Everything works, but once after setting the system time zone to utc-6, when I checks the current system time, it shows me the old time itself (though I can see that the system time zone has changed)
Looks like something like caching. Is there anything like that? Or Is there anything wrong with my script?
Can someone please lead me through a good way?
powershell uses the current locale culture and timezone when it prints.. The current locale is loaded when powershell is invoked.
You are outputing UTC-6 whether ydaylight saving is or is not in effect.
Write-Host "UTC-6 = "$desttime
Which means if a UTC-5 without daylight saving is the current time zone, your script will change time zone but not impact the actual displayed time.
Write-Host "UTC-6 = "$desttime
Which means if a UTC-5 without daylight saving is the current time zone, your script will change time zone but not impact the actual displayed time.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
footech's answer resolves the problem