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.


Discover more from SharePains

Subscribe to get the latest posts sent to your email.

Avatar of Pieter Veenstra

Is your business still running on paper trails, sprawling Excel files, or ageing Access databases? There's a better way — and I can show you exactly what it looks like. I'm the Technical Director of Vantage 365, a Microsoft solutions consultancy working with clients across the UK, the Netherlands, and worldwide. For over 30 years I've been turning messy, manual business processes into clean, automated systems that save time, reduce errors, and give teams the visibility they need to make better decisions. SharePains is not just any blog run by a Microsoft MVP. Have you ever used Try-Catch in Power Automate? The original post about Try-Catch in Power Automate can still be found on this site, https://sharepains.com/2018/02/07/try-catch-finally-in-power-automate-flow/ Or have you ever used the Pieter’s method to avoid variables and speed up your flows? https://sharepains.com/2020/03/11/pieters-method-for-advanced-in-flows/ You can contact me using contact@sharepains.com

Related Posts

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.

Discover more from SharePains

Subscribe now to keep reading and get access to the full archive.

Continue reading