Powershell showing old time after changing the time zone

Here's my powershell script:

$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"
}

Open in new window


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?
Vysakh VenugopalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
powershell uses the current locale culture and timezone when it prints.. The current locale is loaded when powershell is invoked.
0
arnoldCommented:
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.
0
footechCommented:
It does cache data.  You can clear the cache by running
[System.TimeZoneInfo]::ClearCachedData()
Then
[System.TimeZoneInfo]::Local
will show you the changed time zone without restarting PowerShell.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David Johnson, CD, MVPOwnerCommented:
footech's answer resolves the problem
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Server 2012

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.