How to set Timezone and Region to all computers of Active Directory

I needed to massively change the region and timezone settings for all computers. To do this, I created a small script for Powershell:

# Get timezone and region settings from the current computer
$timezone = Get-TimeZone | Select-Object -ExpandProperty Id
$region = (Get-Culture).Name

# Print timezone and region settings to console
Write-Host "Current timezone: $timezone"
Write-Host "Current region: $region"

# Get a list of currently online computers in Active Directory
$onlineComputers = Get-ADComputer -Filter {Enabled -eq $true} -Properties OperatingSystem,OperatingSystemVersion | Where-Object { Test-Connection $_.Name -Count 1 -Quiet }

# Loop through the list of online computers and set the timezone and region
foreach ($computer in $onlineComputers) {
    $computerName = $computer.Name
    $currentRegion = Invoke-Command -ComputerName $computerName -ScriptBlock {
        (Get-Culture).Name
    }
    Write-Host "Current region on $computerName: $currentRegion"
    Write-Host "Setting timezone and region on $computerName..."
    Invoke-Command -ComputerName $computerName -ScriptBlock {
        param($tz, $rgn)
        Set-TimeZone -Id $tz -PassThru
        Set-Culture -CultureInfo $rgn -ErrorAction SilentlyContinue
    } -ArgumentList $timezone, $region
}

# Display a message once the settings have been applied to all computers
Write-Host "Timezone and region settings applied to all online computers in Active Directory.

Description of the PowerShell code:

  1. First, the script retrieves the current timezone and region settings from the local computer using the Get-TimeZone and Get-Culture cmdlets.
  2. Then, the current timezone and region settings are displayed on the console using the Write-Host cmdlet.
  3. The script uses the Get-ADComputer cmdlet to retrieve a list of all currently enabled computers in the Active Directory domain. It filters the list to include only online computers by checking their connectivity status with the Test-Connection cmdlet.
  4. The script loops through each online computer in the list and performs the following actions:
    • The name of the current computer is stored in a variable called $computerName.
    • The script retrieves the current region setting for the remote computer using the Invoke-Command cmdlet and the Get-Culture cmdlet wrapped in a script block.
    • The current region setting for the remote computer is displayed on the console using the Write-Host cmdlet.
    • The script sets the new timezone and region settings for the remote computer using the Invoke-Command cmdlet and the Set-TimeZone and Set-Culture cmdlets wrapped in a script block.
  5. After all the online computers have been processed, a final message is displayed on the console using the Write-Host cmdlet to indicate that the timezone and region settings have been applied to all online computers in Active Directory.
See also  iLo server power on Python script for Telegram Bot

This script allows you to update the timezone and region settings on multiple computers in Active Directory at once, and also provides feedback on the current region setting for each computer before and after the new settings are applied.

 

Author: admin

Leave a Reply

Your email address will not be published. Required fields are marked *