functionpowershelllocalesharepoint-online

How to create a script to change all sites in SharePoint Online to UK locale


When creating a new SPO site in our tenant, the site is created with correct UK time zone, but the region locale is set to US, which means that the date formats, first day of the week etc are in US format. This causes problems with files uploaded there, containing dates etc.

I have a PowerShell to change locale per site:

Connect-PnPOnline https://tenant.sharepoint.com/sites/yoursite
$LocaleId = 2057 # UK
$TimeZoneId = 2 # London

$web = Get-PnPWeb -Includes RegionalSettings,RegionalSettings.TimeZones
$timeZone = $web.RegionalSettings.TimeZones | Where-Object {$_.Id -eq $TimeZoneId}
$web.RegionalSettings.LocaleId = $LocaleId
$web.RegionalSettings.TimeZone = $timeZone
$web.Update()
Invoke-PnPQuery

This code should update individual site (not tested yet).

I would like the code to iterate through all existing SharePoint sites and change the locale of all US to UK locale as per code above, rather than a single site.

What would be the best way to do that? Or may be convert it into function from the code?

Any help appreciated.


Solution

  • You could try the below script to loop through all sites and change the locale

    $LocaleId = 2057 # UK
    $TimeZoneId = 2 # London
    $credentials = Get-Credential
    Connect-PnPOnline https://tenant.sharepoint.com/sites/yoursite -Credentials $credentials
    $sites=Get-PnPTenantSite
    foreach($site in $sites){
    Connect-PnPOnline $site.Url -Credentials $credentials
    $web = Get-PnPWeb -Includes RegionalSettings,RegionalSettings.TimeZones
    $timeZone = $web.RegionalSettings.TimeZones | Where-Object {$_.Id -eq $TimeZoneId}
    $web.RegionalSettings.LocaleId = $LocaleId
    $web.RegionalSettings.TimeZone = $timeZone
    $web.Update()
    Invoke-PnPQuery
    }