kwizcom banner advertisement

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.

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


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}

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

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
We can set the time zone for all 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
SharePoint Online: Change Time Zone using PowerShell SharePoint Online: Change Time Zone using PowerShell Reviewed by Salaudeen Rajack on June 20, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.