question

Myzen2-9930 avatar image
0 Votes"
Myzen2-9930 asked MZ-4230 commented

How to change Timezone for all Sharepoint Sites or Teams

Hello everybody,

I have near 600 SharePoint Online sites which are in a bad timezone.
I just ask to the community if it is possible to change with a powershell script the Regional Settings automatically.

Can you help me please?

Thank you.

office-sharepoint-online
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Sorry but I forgot to tell you that my tenant authentication is using MFA and some scripts are not working when I enter $Username and $Password.

0 Votes 0 ·

1 Answer

ChelseaWu-MSFT avatar image
0 Votes"
ChelseaWu-MSFT answered MZ-4230 commented

Here is a sample script to change tenant wide time zone settings for your reference:

 #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
     }
 }
    
    
 #Parameters
 $AdminSiteURL = "https://<tenant>-admin.sharepoint.com/"
 $TimezoneName = "(UTC) Coordinated Universal Time"
    
 #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
 }

Please remember to change the parameters corresponding to your tenant’s name and required time zone, and also the path of the SharePoint CSOM Assemblies if necessary.

Here is another sample script to get the available time zones in SharePoint Online:

 #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://<tenant>.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

Reference: SharePoint Online: Change Time Zone using PowerShell.

Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


If an Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 8
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for this answer but I have a server message : error 403 forbidden.
Do you know why?
I am not Tenant admin perhaps should I?

0 Votes 0 ·

Please use Comment instead of Write an Answer section to post the reply unless when you provide a valid solution to the question. We will be converting this reply for you.


Yes, you need to be a global administrator in this tenant to run the script.


Here are a few more things to check just in case the issue remains after:

  1. Make sure you provide the correct URL and credentials during execution.

  2. Make sure you have properly installed SharePoint Online Client Component SDK on your client computer. Also make sure you have the latest version installed and the referenced DLL paths are correct.

  3. Make sure you have permission to all sites (besides being granted with global admin role).

  4. Make sure you are working on a compliant device as per the conditional access policies of your tenant if any.

0 Votes 0 ·

Hi @Myzen2-9930 , do you have any progress on this thread? Please feel free to let us know if you need further assistance.

0 Votes 0 ·

Hello,
I'm not global admin and I asked to him to try this script.
So I really don't know if it works.
Thanks for your help.
Do you know if it'll be a restriction from Microsoft?
Thanks a lot.
Have a nice day.

0 Votes 0 ·
Show more comments