Skip to content

Set-TimeZoneConfig

Windows: Sets the system time zone to a specified ID

#Requires -Version 5.1

[CmdletBinding()]
Param
(
    [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
    [string]$Id,

    [string]$ComputerName = $env:COMPUTERNAME,

    [pscredential]$Credential
)

Process
{
    try
    {
        $scriptBlock = {
            Param($TimeZoneId)
            Set-TimeZone -Id $TimeZoneId -ErrorAction Stop
            Get-TimeZone | Select-Object Id, DisplayName, BaseUtcOffset
        }

        if ($ComputerName -ne $env:COMPUTERNAME)
        {
            $invokeParams = @{
                'ComputerName' = $ComputerName
                'ScriptBlock'  = $scriptBlock
                'ArgumentList' = $Id
                'ErrorAction'  = 'Stop'
            }
            if ($null -ne $Credential)
            {
                $invokeParams.Add('Credential', $Credential)
            }

            $result = Invoke-Command @invokeParams
        }
        else
        {
            $result = &$scriptBlock -TimeZoneId $Id
        }

        $output = [PSCustomObject]@{
            Id           = $result.Id
            DisplayName  = $result.DisplayName
            ComputerName = $ComputerName
            Action       = "Set"
            Timestamp    = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        }

        Write-Output $output
    }
    catch
    {
        throw
    }
}

Specifies the identifier of the time zone (e.g., "Pacific Standard Time"). Use Get-TimeZone -ListAvailable to see valid IDs.

Specifies the name of the target computer. Defaults to the local computer.

Specifies a PSCredential object for remote connection.

An interactive directory of PowerShell scripts.