SharePoint Online: Change Time Zone using PowerShell

Requirement: Change Timezone in SharePoint Online.

How to Change Timezone in SharePoint Online?
As a global organization operating in different regions and time zones, we wanted to set time zones for respective sites since users get annoyed when they see Pacific time zones in uploaded document's time stamps. SharePoint Online timezone settings can be set by going to :
  • Site Settings >> Click on "Regional Settings" under "Site Administration"
  • In Regional settings page, You can set time zone, locale and other regional settings.
    sharepoint online change timezone powershell
  • Click on OK located at the bottom right of the page to save your changes.
Please note, You must have Site Collection Administrator rights on all SharePoint Online Site collections in order to change the Timezone Regional settings. And you need SharePoint Online Administrator or Tenant Admin to apply it on tenant wide.

Get a List of All Available Time Zones in SharePoint Online: 
We need either the ID of the time zone or description in order to update timezone of a SharePoint Online site. To get list of time zones, you can either pick it from the Time zones drop down of the above page or use this PowerShell to get all available time zones list.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#SharePoint Online Site URL
$SiteURL = "https://crescent.sharepoint.com/"

#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
#Set up the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Ctx.Credentials = $credentials

#Get all available time zones
$Timezones = $Ctx.Web.RegionalSettings.TimeZones
$Ctx.Load($Timezones)
$Ctx.ExecuteQuery()

#Get Timezone ID and Description
$Timezones | Select ID, Description
This retrieves all available time zones ID and description fields.
sharepoint online get timezone
You can also refer this Microsoft article for available timezone values: https://docs.microsoft.com/en-us/previous-versions/office/sharepoint-server/ms453853(v=office.15)?redirectedfrom=MSDN

SharePoint Online: Change Timezone using PowerShell
Once you obtained the time zone ID or description, You can change the time zone of a site using PowerShell script.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Config parameters for SharePoint Online Site URL and Timezone description
$SiteURL = "https://crescent.sharepoint.com/"
$TimezoneName ="(UTC+04:00) Abu Dhabi, Muscat"

#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
#Set up the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Ctx.Credentials = $credentials

#Get all available time zones
$Timezones = $Ctx.Web.RegionalSettings.TimeZones
$Ctx.Load($Timezones)
$Ctx.ExecuteQuery()

#Filter the Time zone to update
$NewTimezone = $Timezones | Where {$_.Description -eq $TimezoneName}

#sharepoint online powershell set time zone
$Ctx.Web.RegionalSettings.TimeZone = $NewTimezone
$Ctx.Web.Update()
$Ctx.ExecuteQuery() 

PnP PowerShell to Change Timezone in SharePoint Online:
#Set Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$TimezoneName = "(UTC+04:00) Abu Dhabi, Muscat"

#Connect to SharePoint Online with PnP PowerShell
Connect-PnPOnline $SiteURL -UseWebLogin

#Get the Web
$web = Get-PnPWeb -Includes RegionalSettings.TimeZones

#Get the time zone
$Timezone  = $Web.RegionalSettings.TimeZones | Where {$_.Description -eq $TimezoneName}

If($Timezone -ne $Null)
{
    #Update time zone of the site
    $Web.RegionalSettings.TimeZone = $Timezone
    $Web.Update()
    Invoke-PnPQuery
    Write-host "Timezone Updated Successfully!" -ForegroundColor Green
}
else
{
    Write-host "Timezone $TimezoneName not found!" -ForegroundColor Yellow
}
Similarly, you can update Locale, Time format and other regional settings:
$Ctx.web.RegionalSettings.time24 = $True
$Ctx.web.RegionalSettings.LocaleId = 1043
This PowerShell sets default timezone  of the SharePoint Online site.

Set Time Zone for SharePoint Online Site Collection using PowerShell:
As time zone settings are managed at site (web) scope, setting time zone on each and every site is cumbersome. Time zone settings at the top level site collection doesn't affect/propagate automatically to sub-sites. So lets change timezone for SharePoint Online site collection using PowerShell.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#function to change Timezone regional settings of a SharePoint Online site
Function Set-SPOTimeZone
{ 
    Param ([Microsoft.SharePoint.Client.Web]$Web,[Microsoft.SharePoint.Client.TimeZone]$Timezone)

    Try
    {
        #Update the timezone of the site
        $Web.RegionalSettings.TimeZone = $NewTimezone
        $Web.Update()
        $Ctx.ExecuteQuery()

        Write-host -f Green "Timezone has been updated for "$Web.Url

        #Get all subsites of the web
        $Ctx.Load($Web.Webs)
        $Ctx.executeQuery()

        #Iterate through each subsites and call the function recursively
        Foreach ($Subweb in $Web.Webs)
        {
            #Call the function to set Timezone for the web
            Set-SPOTimeZone -Web $Subweb -Timezone $NewTimezone
        }
    }
    Catch [System.Exception]
    {
        Write-Hoste -f Red $_.Exception.Message
    }  
}

#Config parameters for SharePoint Online Site Collection URL and Timezone description
$SiteURL = "https://crescent.sharepoint.com/"
$TimezoneName ="(UTC+04:00) Abu Dhabi, Muscat"

#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
#Set up the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Ctx.Credentials = $credentials
  
#Get the Root web from given Site Collection URL
$Web = $Ctx.web
$Ctx.Load($Web)

#Get the Time zone to update
$Timezones = $Ctx.Web.RegionalSettings.TimeZones
$Ctx.Load($Timezones)
$Ctx.ExecuteQuery()
$NewTimezone = $Timezones | Where {$_.Description -eq $TimezoneName}
 
#Call the function to set Timezone for the Root web
Set-SPOTimeZone -Web $web -Timezone $NewTimezone

Change Timezone in SharePoint Online - Tenant wide
To set the default timezone in SharePoint Online, do the following:
  • Login to SharePoint Admin Center >> Click on "Settings" in the left navigation >> Click on "Site Creation".
  • In the Site Creation page, Select the default timezone for new site collections.
    set default timezone for sharepoint online
This sets the default time zone for new site collections you create.

How about existing site collections?
We can set the time zone for all existing site collection in SharePoint Online - tenant wide using below PowerShell script. This script simply loops through all sites and changes the default timezone. Just set $AdminSiteURL and $TimezoneName variables in the script and provide the credentials to connect to SharePoint Online Admin center once.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#function to change Timezone regional settings of a SharePoint Online site
Function Set-SPOnlineTimeZone([String]$SiteURL,[String]$TimezoneName,[PSCredential]$Cred)
{ 
     Try
     {
        #Setup Credentials to connect
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
        #Set up the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $credentials
  
        #Get the Root web from given URL
        $Web = $Ctx.web
        $Ctx.Load($Web)

        #Get the Time zone to update
        $Timezones = $Web.RegionalSettings.TimeZones
        $Ctx.Load($Timezones)
        $Ctx.ExecuteQuery()
        $NewTimezone = $Timezones | Where {$_.Description -eq $TimezoneName}
 
        #Update the timezone of the site
        $Web.RegionalSettings.TimeZone = $NewTimezone
        $Web.Update()
        $Ctx.ExecuteQuery()

        Write-host -f Green "Timezone has been updated for "$Web.Url

        #Get all subsites of the web
        $Ctx.Load($Web.Webs)
        $Ctx.executeQuery()

        #Iterate through each subsites and call the function recursively
        Foreach ($Subweb in $Web.Webs)
        {
            #Call the function to set Timezone for the web
            Set-SPOnlineTimeZone -SiteURL $Subweb.URL -TimezoneName $TimezoneName -Cred $AdminCredentials
        }
   }
    Catch [System.Exception]
    {
        Write-Host -f Red $_.Exception.Message
    } 
} 

#Config parameters for SharePoint Online Admin Center and Timezone description
$AdminSiteURL = "https://crescent-admin.sharepoint.com/"
$TimezoneName ="(UTC+04:00) Abu Dhabi, Muscat"

#Get credentials to connect to SharePoint Online Admin Center
$AdminCredentials = Get-Credential

#Connect to SharePoint Online Tenant Admin
Connect-SPOService -URL $AdminSiteURL -Credential $AdminCredentials

#Get all Site Collections
$SitesCollection = Get-SPOSite -Limit ALL

#Iterate through each site collection
ForEach($Site in $SitesCollection)
{
    Write-host -f Yellow "Setting Timezone for Site Collection:"$Site.URL

    #Call the function to set Timezone for the site
    Set-SPOnlineTimeZone -SiteURL $Site.URL -TimezoneName $TimezoneName -cred $AdminCredentials
}
Here is my another post on setting time zone and other regional settings in SharePoint On-premises Change Regional Settings, Time zone, Locale in SharePoint 2013 using PowerShell

6 comments:

  1. Do you aware any impact for the timezone settings? The timezone setting seems not related to any SharePoint features. The OOTB usage reports are using GMT and have no way to change....

    ReplyDelete
  2. I'm getting: Exception calling "ExecuteQuery" with "0" argument(s): "The sign-in name or password does not match one in the Microsoft account system." error!

    ReplyDelete
    Replies
    1. When you use two-factor authentication, Leave the -credential parameter to Connect-SPOService cmdlet. You'll get a prompt (which is 2FA aware) to enter your user name and password. Or use PnP PowerShell to handle two-factor authentication.

      Delete
  3. I'm just getting: The remote server returned an error: (403) Forbidden. on every site I'm not a member of. I have MFA enabled and can't work out what's required to change the Tenant wide script to work with MFA.

    ReplyDelete
  4. Just an update from me to say I worked around the MFA issue using an App Password. I then also needed to add myself as an 'Additional Site Admin' which I did as part of the script.

    ReplyDelete
    Replies
    1. Yes! App password works. Also, You can skip "Credential" parameter from Connect-SPOService cmdlet, So that you'll get a popup to enter your credentials which is MFA aware.
      E.g. Connect-SPOService -URL $AdminSiteURL

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.