≡ Menu

PowerShell: Get time zone information remotely and export to CSV

This post tasks about fetching the time zone information from remote(as well as local) computers using PowerShell. We will be using two approaches to fetch this information. First one is using WMI classes and other is using the PowerShell built-in cmdlet.

First we will see how to fetch this information using WMI classes since it works with all versions of Windows. Time zone information in windows can be accessed via Win32_TimeZone WMI class. Apart from time zone, this class provide various other information as well. You can read more about this class at MSDN(https://msdn.microsoft.com/en-us/library/aa394498(v=vs.85).aspx). I will limit this article to time zone details only. We will be fetching this information from Caption property.

Fetch local time zone using WMI query

Get-WMIObject -Class Win32_TimeZone

Fetch remote time zone using WMI Query

Get-WMIObject -Class Win32_TimeZone -Computer Server01

You can write a simple script around the below command to fetch this information for list of computers. The output of this script will be exported to CSV for ease of viewing the results.

Script:

[CmdletBinding()]
param(
    [string[]]$ComputerName =  $env:COMPUTERNAME
)
$Col = @()
foreach($Computer in $ComputerName) {
    if(!(Test-Connection -ComputerName $Computer -count 1 -Quiet)) {
        Write-Host "$Computer : Not Reachable" -ForegroundColor Yellow
        Continue
    }

    try {
        $tzinfo = Get-WMIObject -Class Win32_TimeZone -ComputerName $Computer -ErrorAction Stop
        $Col += $tzinfo | select-Object PSComputerName, Caption, StandardName, DaylightName
        Write-Host "$computer : Successfully fetched the time zone info" -ForegroundColor Green
    } catch {
        Write-Host "$Computer : Failed to fetch the Time zone info" -ForegroundColor Yellow
    }
}

$Col | export-csv -Path "c:\temp\timezoneinfo.csv" -NoTypeInformation


How to run the script?

#Read the servers list from text file to a variable
$servers = Get-Content c:\temp\servers.txt
#Run the script by passing servers list to it
.\Get-TimeZoneInfo.ps1 -ComputerName $servers

Output

Second approach is to use built-in cmdlets. PowerShell comes with two cmdlets Get-TimeZone & Set-TimeZone starting PowerShell v5.1. We will be using Get-TimeZone to see how can retrieve the information.

Running this cmdlet without any arguments gives the time zone information of local computer. If you need to run this to fetch remotely there is no -ComputerName parameter available. However, you can PowerShell Remoting to execute it on remote host. Make sure that remote system also must be running PowerShell v5.1 or higher. Otherwise this will not work.

Fetch local time zone using cmdlet

Get-TimeZone

Fetch Remote time zone using cmdlet

Invoke-Command -ComputerName SERVER01 -ScriptBlock { Get-TimeZone }

You can write a small script around the above command to run it against the multiple computers. The approach to do it is similar to what we have done for WMI based script above. You can give a try and post in comments section if you need help.

Hope this helps.

Comments on this entry are closed.

  • Turbofroggy August 28, 2018, 1:17 am

    Thank you! I could not for the life of me figure out why the Get-Timezone and Set-Timezone wasn’t working. This is the error I was getting on a Windows Server 2012 R2 machine:
    “The term ‘Get-TimeZone’ is not recognized as the name of a cmdlet, function, script file, or operable program. ”
    Installed WMF 5.1, fixed the issue. Thanks!

  • Roberto Canseco April 6, 2020, 1:10 am

    How can I integrate the following command “Get-Date -format r” to extract the data I want in the .csv
    Congratulations on your publication, I hope you can help me.

  • Fred August 11, 2020, 12:12 pm

    Can you take it further and return timezone from a VPN destination? I’d like to take that TimeZone and set local system to same. TIA