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:
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
Write-Host “Found mapping for” $up[“Office”]
$up[“SPS-TimeZone”].Value = $web.RegionalSettings.TimeZones[$i];
$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.