SharePoint 2013 – Update the time zone with PowerShell

Ok, so today I’ve got a customer with users all over the world. Initially the time for everybody was the server time, which is fine for some but not for everybody.

Changing 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.

 

 

Advertisements

2 thoughts on “SharePoint 2013 – Update the time zone with PowerShell

  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 ?

    Like

Please leave a comment or feedback

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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