We are a daughter company located in another country than our parent company which means that our shared office 365 tenant has a default language that we don’t daily use.
To not have to change the language settings of every user individually we can use the following script to bulk edit the users based on OU membership.
NOTE! This is only for the language settings for example the built in excell in onedrive. This does not change the preferred office365 language.
If you have any questions of how it works, contact me.
$UpnPath = "C:\OneDriveLanguageScript\UsersToCheck.txt"
$DoneUpnPath = "C:\OneDriveLanguageScript\DoneUsers.txt"
$UpnPath = "C:\OneDriveLanguageScript\UsersToCheck.txt"
$SiteURL = "https://tehoc-admin.sharepoint.com/"
$ODUrl = "https://tehoc-my.sharepoint.com/personal/"
$Locale = "1053"
#User Name Password to connect
$AdminUserName = "username@domain.com"
$AdminPassword = "apppass" #App Password
#Prepare the Credentials
$SecurePassword = ConvertTo-SecureString $AdminPassword -AsPlainText -Force
$creds = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminUserName, $SecurePassword
$C = Get-ADUser -Filter "userPrincipalName -like '*'" -SearchBase 'OU=SWE,DC=domain,DC=com' | Select-Object -Property UserPrincipalName | Out-String
#Creates file with all users UPN
if (!(Test-Path $UpnPath))
{
New-Item -Path "C:\OneDriveLanguageScript\" -Name "UsersToCheck.txt" -ItemType "file" -Value $C
Write-Host "Created new file and text content added"
}
else
{
Remove-Item –path $UpnPath
New-Item -Path "C:\OneDriveLanguageScript\" -Name "UsersToCheck.txt" -ItemType "file" -Value $C
Write-Host "File already exists so it was cleared and filled with new data"
}
(Get-Content $UpnPath | Select-Object -Skip 3) | Set-Content "C:\OneDriveLanguageScript\UsersToCheck.txt"
Write-Host "Removing first three rows"
(Get-Content $UpnPath | Foreach {$_.TrimEnd()}) | Set-Content "C:\OneDriveLanguageScript\UsersToCheck.txt"
Write-Host "removing trailing space of file"
(Get-Content $UpnPath | ? {$_.trim() -ne "" }) | Set-Content "C:\OneDriveLanguageScript\UsersToCheck.txt"
Write-Host "removing trailing empty lines"
#Check if there is a file with done users
if (!(Test-Path $DoneUpnPath))
{
New-Item -Path "C:\OneDriveLanguageScript\" -Name "DoneUsers.txt" -ItemType "file"
Write-Host "Created new DoneUsers file and text content added"
}
#Compare files
$strReference = Get-Content $UpnPath
$strDifference = Get-Content $DoneUpnPath
#If null array.... ffs....
if ($strReference -eq $null) { $strReference = "" }
if ($strDifference -eq $null) { $strDifference = "" }
#Removes empty inputs
$strReference = $strReference.Where({ $_ -ne "" })
$strDifference = $strDifference.Where({ $_ -ne "" })
#Compares the lists
$users = Compare-Object $strReference $strDifference
Write-Host "Users to change language on"
Write-Host $users.InputObject
#Connect msol service
Connect-SPOService -Url $SiteURL -Credential $creds
#Add references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll"
foreach ($user in $users.InputObject)
{
#Building user ODrive Full Url
Write-Host "Building ODrive Full Url for $user" -ForegroundColor Yellow
$ODriveFullUrl = $ODUrl + $user.Replace("@","_").replace('.','_')
Write-Host $ODriveFullUrl
#Adding Admin access to user OneDrive
Write-Host "Adding Admin access to $user OneDrive" -ForegroundColor Yellow
Set-SPOUser -Site $ODriveFullUrl -LoginName $creds.UserName -IsSiteCollectionAdmin $true | Out-Null
#Bind to OD4B Site and change locale
Write-Host "Changing Locale for $user" -ForegroundColor Yellow
$spocreds = [Microsoft.SharePoint.Client.SharePointOnlineCredentials]::new($Creds.UserName,$creds.Password)
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($ODriveFullUrl)
$return = $Context.Credentials = $spocreds
$Context.ExecuteQuery()
$Context.Web.RegionalSettings.LocaleId = $Locale
$Context.Web.Update()
$Context.ExecuteQuery()
Write-Host $Context
#Removing Admin access from User OneDrive
Write-Host "Removing Admin access from $upn OneDrive" -ForegroundColor Green
Set-SPOUser -Site $ODriveFullUrl -LoginName $creds.UserName -IsSiteCollectionAdmin $false | Out-Null
Add-Content $DoneUpnPath $user
}