PowerTip: Display the Current Time and Date on a Remote Computer

Summary: See how to use Windows PowerShell to display the current date and time on a remote computer.

Hey, Scripting Guy! Question How can I display the current date and time on a remote computer by using Windows PowerShell?

               Hey, Scripting Guy! Answer Use Windows PowerShell remoting, and the Invoke-Command cmdlet to run Get-Date.

                                     invoke-command -ComputerName dc1 -ScriptBlock {get-date}

                             A shorter version of this command is shown here:

                                     icm dc1 {get-date}

Comments (5)

  1. User1 says:

    This doesn’t work

  2. Chris Demeur says:

    The invoke command assumes you have WinRM installed and running on the remote computer. In my case I am trying to get the times of domain computers on the network from the server. I’ll have to find another way.

  3. anonymous says:

    if you dont have winRM, use this: net time \$targetServer

  4. User2 says:

    Couldn’t you also use this option? gwmi -ComputerName -Class Win32_LocalTime

  5. wkimblejr says:

    In case anyone else visits here, I, too, had the same issue. What happens is when you use something like above:

    invoke-command -ComputerName dc1 -ScriptBlock {get-date}

    or even

    (Get-WmiObject -Class Win32_OperatingSystem -computername dc1).LocalDateTime

    PowerShell converts the date received into the time of the localhost. In order to get around that, I used the Get-WmiObject method instead of Get-Date and grabbed the Time Zone property for both the localhost and remote host(s):

    # Store remote wmi object in variable for easy access throughout script.
    $Win32OSVar = (Get-WmiObject -Class Win32_OperatingSystem -computername dc1)

    # This next var grabs the LocalDateTime property from the WMI object above and converts it into something readable. It’s still not accurate, though.
    $FakeRemoteTime= $Win32OSVar.ConvertToDateTime($Win32OSVar.LocalDateTime)

    # TimeZone Vars
    $RHtz = $Win32OSVar.CurrentTimeZone
    $LHtz = (Get-WmiObject -Class Win32_OperatingSystem).CurrentTimeZone

    # Now, perform some EASY math to get the difference in minutes between TZs
    $TZDiff = $RHtz – $LHtz

    # Finally, add that difference to $FakeRemoteTime
    $RealRemoteTime = $FakeRemoteTime.AddMinutes($TZDiff)

    The great thing about the CurrentTimeZone property is that the value returned is formatted in minutes difference from what I believe is UTC, though I’m not sure it’s accurate…. However, it doesn’t really matter. So as long as you know what time zones the value returned correlate with, you’re good to go.

    For example, it’s 3:00PM, the CurrentTimeZone property for localhost returns -360 and I have a device in PST that returns -420. -420 -(-360) = -60. Add that to what Powershell returns from the remote machine (i.e, my localhost time) and you get 2:00PM.

Skip to main content