#Requires -Version 7.4 #Requires -Modules PnP.PowerShell <# .SYNOPSIS Applies regional settings (locale, time zone, time format, work week) to one or more SharePoint Online sites, with a log and a before/after report. .DESCRIPTION For each site, the script: 1. connects with PnP.PowerShell; 2. reads the current regional settings; 3. works out the requested changes (command-line parameters, overridden site by site by the CSV columns if you use a CSV); 4. applies only what changes, then reads the site again to check; 5. writes one row per setting to the CSV report: before, after, status. The whole run is also recorded in a log (Start-Transcript). With -WhatIf, nothing is changed: the report shows what would change. .PARAMETER SiteUrl One or more site URLs. Use without -CsvPath. .PARAMETER CsvPath CSV file (; separator) with a Url column and, if needed, LocaleId, TimeZoneId, Time24, FirstDayOfWeek, WorkDays, WorkDayStart, WorkDayEnd columns. An empty cell falls back to the command-line value. .PARAMETER ClientId ID of the Entra ID app used by PnP.PowerShell (see the prerequisites). .PARAMETER LocaleId Locale (date and number format), for example 2057 = English (United Kingdom), 1033 = English (United States), 1036 = French (France). .PARAMETER TimeZoneId SharePoint time zone ID, for example 2 = London, 3 = Paris. Get-PnPTimeZoneId -Match London returns the ID for a city. .PARAMETER Time24 $true for the 24-hour clock, $false for the 12-hour format (AM/PM). .PARAMETER FirstDayOfWeek First day of the week: Monday, Sunday... .PARAMETER WorkDays Work days: Monday, Tuesday, Wednesday, Thursday, Friday. .PARAMETER WorkDayStart Start of the work day, HH:mm format (08:00). .PARAMETER WorkDayEnd End of the work day, HH:mm format (17:00). .PARAMETER ReportFolder Folder for the log and the report. Default: your Documents folder. .EXAMPLE # 1) Dry run on two sites: nothing is changed .\Set-SPRegionalSettings-en.ps1 ` -SiteUrl "https://contoso.sharepoint.com/sites/London", "https://contoso.sharepoint.com/sites/Manchester" ` -ClientId "00000000-0000-0000-0000-000000000000" ` -LocaleId 2057 -TimeZoneId 2 -Time24 $true -FirstDayOfWeek Monday -WhatIf .EXAMPLE # 2) One setting per site, described in a CSV .\Set-SPRegionalSettings-en.ps1 -CsvPath .\sites.csv -ClientId "00000000-0000-0000-0000-000000000000" #> [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Mandatory, ParameterSetName = "Sites")] [string[]] $SiteUrl, [Parameter(Mandatory, ParameterSetName = "Csv")] [string] $CsvPath, [Parameter(Mandatory)] [string] $ClientId, [int] $LocaleId, [int] $TimeZoneId, [Nullable[bool]] $Time24, [Nullable[System.DayOfWeek]] $FirstDayOfWeek, [System.DayOfWeek[]] $WorkDays, [string] $WorkDayStart, [string] $WorkDayEnd, [string] $ReportFolder = (Join-Path $HOME "Documents") ) $ErrorActionPreference = "Stop" # ---------------------------------------------------------------------------- # Conversions to the SharePoint format # ---------------------------------------------------------------------------- # Work hours are stored as minutes since midnight: 08:00 = 480. function ConvertTo-Minutes([string] $Time) { $span = [TimeSpan]::ParseExact($Time, "hh\:mm", [Globalization.CultureInfo]::InvariantCulture) [int] $span.TotalMinutes } # Work days are a 7-bit mask read from Sunday (64) to Saturday (1): # Monday to Friday = 0111110 = 62. function ConvertTo-WorkDayMask([System.DayOfWeek[]] $Days) { $mask = 0 foreach ($d in $Days) { $mask = $mask -bor (64 -shr [int]$d) } $mask } function ConvertFrom-WorkDayMask([int] $Mask) { $names = foreach ($d in 0..6) { if ($Mask -band (64 -shr $d)) { [System.DayOfWeek]$d } } ($names -join ",") } # ---------------------------------------------------------------------------- # Site list and the settings wanted for each one # ---------------------------------------------------------------------------- $defaults = [ordered]@{ LocaleId = if ($PSBoundParameters.ContainsKey("LocaleId")) { $LocaleId } else { $null } TimeZoneId = if ($PSBoundParameters.ContainsKey("TimeZoneId")) { $TimeZoneId } else { $null } Time24 = $Time24 FirstDayOfWeek = $FirstDayOfWeek WorkDays = if ($WorkDays) { ConvertTo-WorkDayMask $WorkDays } else { $null } WorkDayStart = if ($WorkDayStart) { ConvertTo-Minutes $WorkDayStart } else { $null } WorkDayEnd = if ($WorkDayEnd) { ConvertTo-Minutes $WorkDayEnd } else { $null } } if ($CsvPath) { $rows = Import-Csv -Path $CsvPath -Delimiter ";" -Encoding UTF8 } else { $rows = $SiteUrl | ForEach-Object { [PSCustomObject]@{ Url = $_ } } } $targets = foreach ($row in $rows) { $wanted = [ordered]@{} foreach ($key in $defaults.Keys) { $cell = if ($row.PSObject.Properties[$key]) { "$($row.$key)".Trim() } else { "" } $wanted[$key] = if ($cell -eq "") { $defaults[$key] } else { switch ($key) { "Time24" { [bool]::Parse($cell) } "FirstDayOfWeek" { [System.DayOfWeek]$cell } "WorkDays" { ConvertTo-WorkDayMask ([System.DayOfWeek[]]($cell -split "\s*,\s*")) } "WorkDayStart" { ConvertTo-Minutes $cell } "WorkDayEnd" { ConvertTo-Minutes $cell } default { [int]$cell } } } } if ($null -ne $wanted.WorkDayStart -and $null -ne $wanted.WorkDayEnd -and $wanted.WorkDayEnd -le $wanted.WorkDayStart) { throw "Site $($row.Url): the work day must end after it starts." } [PSCustomObject]@{ Url = $row.Url.Trim(); Wanted = $wanted } } if (-not ($targets | ForEach-Object { $_.Wanted.Values } | Where-Object { $null -ne $_ })) { throw "No setting requested: give at least one, on the command line or in the CSV." } # ---------------------------------------------------------------------------- # Log and report # ---------------------------------------------------------------------------- if (-not (Test-Path $ReportFolder)) { New-Item -ItemType Directory -Path $ReportFolder | Out-Null } $stamp = Get-Date -Format "yyyyMMdd-HHmmss" $logPath = Join-Path $ReportFolder "Regional-settings-$stamp.log" $reportPath = Join-Path $ReportFolder "Regional-settings-$stamp.csv" $report = [System.Collections.Generic.List[object]]::new() # The log keeps all console output, errors included, even with -WhatIf. Start-Transcript -Path $logPath -WhatIf:$false | Out-Null # Readable values for the report. function Format-Value([string] $Name, $Value, $TimeZones) { if ($null -eq $Value) { return "" } switch ($Name) { "TimeZoneId" { $tz = $TimeZones | Where-Object Id -eq $Value; "$Value $($tz.Description)" } "WorkDays" { "$Value ($(ConvertFrom-WorkDayMask $Value))" } "WorkDayStart" { [TimeSpan]::FromMinutes($Value).ToString("hh\:mm") } "WorkDayEnd" { [TimeSpan]::FromMinutes($Value).ToString("hh\:mm") } default { "$Value" } } } function Get-CurrentSettings { $web = Get-PnPWeb -Includes RegionalSettings, RegionalSettings.TimeZone, RegionalSettings.TimeZones $rs = $web.RegionalSettings [PSCustomObject]@{ Rs = $rs TimeZones = $rs.TimeZones Values = [ordered]@{ LocaleId = [int]$rs.LocaleId TimeZoneId = [int]$rs.TimeZone.Id Time24 = [bool]$rs.Time24 FirstDayOfWeek = [System.DayOfWeek][int]$rs.FirstDayOfWeek WorkDays = [int]$rs.WorkDays WorkDayStart = [int]$rs.WorkDayStartHour WorkDayEnd = [int]$rs.WorkDayEndHour } } } try { foreach ($t in $targets) { Write-Host "`nSite: $($t.Url)" -ForegroundColor Cyan try { # ---------------------------------------------------------------- # 1. Connect and read # ---------------------------------------------------------------- Connect-PnPOnline -Url $t.Url -Interactive -ClientId $ClientId $current = Get-CurrentSettings $rs = $current.Rs # ---------------------------------------------------------------- # 2. What changes # ---------------------------------------------------------------- $changes = @($t.Wanted.Keys | Where-Object { $null -ne $t.Wanted[$_] -and $t.Wanted[$_] -ne $current.Values[$_] }) if ($null -ne $t.Wanted.TimeZoneId -and -not ($current.TimeZones | Where-Object Id -eq $t.Wanted.TimeZoneId)) { throw "Unknown time zone $($t.Wanted.TimeZoneId). Look up the ID with Get-PnPTimeZoneId." } foreach ($name in $t.Wanted.Keys) { if ($null -eq $t.Wanted[$name]) { continue } $report.Add([PSCustomObject][ordered]@{ Date = Get-Date -Format "yyyy-MM-dd HH:mm:ss" Site = $t.Url Setting = $name Before = Format-Value $name $current.Values[$name] $current.TimeZones After = Format-Value $name $t.Wanted[$name] $current.TimeZones Status = if ($name -in $changes) { "To change" } else { "Already compliant" } }) } if (-not $changes) { Write-Host " Nothing to change."; continue } Write-Host (" To change: {0}" -f ($changes -join ", ")) # ---------------------------------------------------------------- # 3. Apply (skipped with -WhatIf) # ---------------------------------------------------------------- $siteRows = $report | Where-Object { $_.Site -eq $t.Url -and $_.Status -eq "To change" } if (-not $PSCmdlet.ShouldProcess($t.Url, "Change $($changes -join ', ')")) { $siteRows | ForEach-Object { $_.Status = "Dry run: not changed" } continue } # Unlike the web interface, changing LocaleId here does not reset the # other settings to their defaults: only the requested fields change. if ("LocaleId" -in $changes) { $rs.LocaleId = $t.Wanted.LocaleId } if ("TimeZoneId" -in $changes) { $rs.TimeZone = $current.TimeZones | Where-Object Id -eq $t.Wanted.TimeZoneId } if ("Time24" -in $changes) { $rs.Time24 = $t.Wanted.Time24 } if ("FirstDayOfWeek" -in $changes) { $rs.FirstDayOfWeek = [uint32]$t.Wanted.FirstDayOfWeek } if ("WorkDays" -in $changes) { $rs.WorkDays = [int16]$t.Wanted.WorkDays } if ("WorkDayStart" -in $changes) { $rs.WorkDayStartHour = [int16]$t.Wanted.WorkDayStart } if ("WorkDayEnd" -in $changes) { $rs.WorkDayEndHour = [int16]$t.Wanted.WorkDayEnd } $rs.Update() Invoke-PnPQuery # ---------------------------------------------------------------- # 4. Check: read the site again # ---------------------------------------------------------------- $after = (Get-CurrentSettings).Values foreach ($row in $siteRows) { $ok = $after[$row.Setting] -eq $t.Wanted[$row.Setting] $row.Status = if ($ok) { "Changed" } else { "Mismatch after update: $(Format-Value $row.Setting $after[$row.Setting] $current.TimeZones)" } } Write-Host " Done." -ForegroundColor Green } catch { # Missing permissions, site not found...: record it and move on. Write-Warning " Failed: $($_.Exception.Message)" $report.Add([PSCustomObject][ordered]@{ Date = Get-Date -Format "yyyy-MM-dd HH:mm:ss"; Site = $t.Url; Setting = "-" Before = ""; After = ""; Status = "Failed: $($_.Exception.Message)" }) } finally { # Disconnect quietly if the connection failed. try { Disconnect-PnPOnline } catch { } } } } finally { # The report is written even if the script stops halfway. $report | Export-Csv -Path $reportPath -Encoding UTF8 -Delimiter ";" -WhatIf:$false $done = @($report | Where-Object Status -eq "Changed").Count Write-Host ("`n{0} setting(s) changed. Report: {1}" -f $done, $reportPath) -ForegroundColor Green Write-Host "Log: $logPath" Stop-Transcript -WhatIf:$false | Out-Null }