Today I have a customer with users all over the world. Initially the time for everybody was the server time but now they needed to update the time zone.

Update the Time Zones

To update the time zone is something that I didn’t want users having to do. So I thought I’ll do that for them with PowerShell.

First a bit about user profiles and time zones.

To get a user profile you have to get a UserProfileManager object out of your site collection

$url = “https:/ /intranet.mycomp.com/”

$site = Get-SPSite $url

$web = $site.RootWeb

$context = Get-SPServiceContext $site

$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)

Then to get all the profiles:

$profiles = $profileManager.GetEnumerator()

And finally I walk through the profiles with the following construction:

while ($profiles.MoveNext())

{

}

Inside the above loop I first collect some information from the user profile

$userProfile = $profiles.Current

$name = $userProfile.DisplayName

$accname = $userProfile.AccountName

$user = $web.AllUsers[$accname]

$up = $profileManager.GetUserProfile($accname);

Yes I am picking up the user profile twice. The first user profile seems to have some fields readonly causing problem later down the line.

Now updating the regional settings I’ve added a csv file which includes a location, timezoneID and timezone description. I’m using this as a lookup table to select the right timezone. For simplicity sake I’m leaving that out of this article. This lookup gives me a $i that gives me the right Timezone.

if ( $up[“SPS-TimeZone”].Value -eq $null)

{

# only changing the time zone if it hasn’t been set before

    $up[“SPS-TimeZone”].Value

    Write-Host “Found mapping for” $up[“Office”]

    $up[“SPS-TimeZone”].Value = $web.RegionalSettings.TimeZones[$i];

    $up.Commit();

    $tz = $up[“SPS-TimeZone”]

    $desc = $tz.Description

    Write-Host “Time zone set to: $desc

}

in the above section you can see that $web.RegionalSettings contains a list with all available Timezones. This list is used to assign a timezone. Why?

It is not possible to create a new SPTimeZone object in PowerShell, but it is possible to assign an existing object to a user profiles TimeZone.

By Pieter Veenstra

Business Applications and Office Apps & Services Microsoft MVP working as a Microsoft Productivity Principal Consultant at HybrIT Services. You can contact me using contact@veenstra.me.uk.

2 thoughts on “Update the time zone with PowerShell in SharePoint”
  1. hello (and sorry for my English), in Office 365, i have to set for all users ‘s profiles French timezone. For the moment it is CANADA for all users. With your script i will be able to change for all users ? it is not in OWA but in profiles, that is true ?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.