{"id":4720,"date":"2012-11-05T00:01:00","date_gmt":"2012-11-05T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/11\/05\/use-powershell-to-easily-compare-the-time-between-two-computers\/"},"modified":"2012-11-05T00:01:00","modified_gmt":"2012-11-05T00:01:00","slug":"use-powershell-to-easily-compare-the-time-between-two-computers","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-easily-compare-the-time-between-two-computers\/","title":{"rendered":"Use PowerShell to Easily Compare the Time Between Two Computers"},"content":{"rendered":"<p><strong>Summary:<\/strong> Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to easily compare the time between two computers.\nMicrosoft Scripting Guy, Ed Wilson, is here. In two days, I will be speaking to the <a href=\"http:\/\/www.azposh.com\/\" target=\"_blank\">AZPOSH Windows PowerShell user group in Phoenix, Arizona<\/a>. No, I am not flying out there&mdash;I have enough travel coming up. Instead, I am speaking via Lync. Lync does an excellent job, and it is just about like being there in person (except for missing all the great restaurants they have out there). I will be speaking about using the Windows PowerShell 3.0 remoting capabilities for management purposes. It will be a lot of fun. If you cannot be there in person (such as myself), you may want to dial in to the Lync meeting.<\/p>\n<h2>Checking local and remote time<\/h2>\n<p>One of the people who was at the <a href=\"http:\/\/powershellgroup.org\/charlotte.nc\" target=\"_blank\">Windows PowerShell user group in Charlotte<\/a> the other day was asking me about checking the time on remote computers. This got me to playing around.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong> &nbsp;&nbsp;For more information about time synchronization issues, see the following Hey, Scripting Guy! Blog posts: <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/05\/09\/hey-scripting-guy-weekend-scripter-configuring-w32time-service-logging.aspx\" target=\"_blank\">Configuring w32Time Service Logging<\/a> and <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/05\/08\/hey-scripting-guy-weekend-scripter-configuring-a-timeserver-in-windows-server.aspx\" target=\"_blank\">Configuring an Authoritative Time Server in Windows Server<\/a>.\nOf course, it is possible to use WMI to query for time information from a remote machine. To do this, use the <strong>Win32_LocalTime WMI<\/strong> class. The problem with this is that each property is a piece of the time and, therefore, must be assembled from at least three different properties (unless you also want the date&mdash;that is another three properties).<\/p>\n<h2>Finding the PDC emulator<\/h2>\n<p>By default, the domain&rsquo;s authoritative time server is the server holding the PDC Emulator FSMO role. The easiest way to find the name of this server is to use the <strong>Get-ADDomain<\/strong> cmdlet from the Active Directory module. On Windows PowerShell 3.0, it is not necessary to load the Active Directory module prior to running the command. Therefore, the following command returns the PDC Emulator for my domain.<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; (Get-ADDomain).pdcemulator<\/p>\n<p style=\"padding-left: 30px\">dc1.iammred.net<\/p>\n<h2>Check the date \/ time on a remote computer<\/h2>\n<p>The easiest way to obtain the date and time on a remote computer is to simply use the <strong>Get-Date<\/strong> cmdlet. By using Windows PowerShell remoting, it is trivial to obtain the remote time information. An example is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; invoke-command -ComputerName dc1 -ScriptBlock {get-date}<\/p>\n<p style=\"padding-left: 30px\">Wednesday, October 31, 2012 1:23:42 PM\nI can shorten the command considerably by taking advantage of the <strong>ICM<\/strong> alias for the <strong>Invoke-Command<\/strong> cmdlet and by using positional parameters, as shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; icm dc1 {get-date}<\/p>\n<p style=\"padding-left: 30px\">Wednesday, October 31, 2012 1:24:24 PM\nIf I would like to see all of the date \/ time information, I can pipe the results to the <strong>Format-List<\/strong> cmdlet. This is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; icm dc1 {get-date} | fl *<\/p>\n<p style=\"padding-left: 30px\">DisplayHint&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : DateTime<\/p>\n<p style=\"padding-left: 30px\">PSComputerName&nbsp;&nbsp;&nbsp;&nbsp; : dc1<\/p>\n<p style=\"padding-left: 30px\">RunspaceId&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 7cf0452c-688a-4e16-83f5-d9b0ec6ded6e<\/p>\n<p style=\"padding-left: 30px\">PSShowComputerName : True<\/p>\n<p style=\"padding-left: 30px\">DateTime&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Wednesday, October 31, 2012 2:33:35 PM<\/p>\n<p style=\"padding-left: 30px\">Date&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 10\/31\/2012 12:00:00 AM<\/p>\n<p style=\"padding-left: 30px\">Day&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 31<\/p>\n<p style=\"padding-left: 30px\">DayOfWeek&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Wednesday<\/p>\n<p style=\"padding-left: 30px\">DayOfYear&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 305<\/p>\n<p style=\"padding-left: 30px\">Hour&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 14<\/p>\n<p style=\"padding-left: 30px\">Kind&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Local<\/p>\n<p style=\"padding-left: 30px\">Millisecond&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 879<\/p>\n<p style=\"padding-left: 30px\">Minute&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 33<\/p>\n<p style=\"padding-left: 30px\">Month&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 10<\/p>\n<p style=\"padding-left: 30px\">Second&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 35<\/p>\n<p style=\"padding-left: 30px\">Ticks&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 634872908158794870<\/p>\n<p style=\"padding-left: 30px\">TimeOfDay&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 14:33:35.8794870<\/p>\n<p style=\"padding-left: 30px\">Year&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 2012\nOn the other hand, it is also possible that what I need to do is to see if there is a time offset between my computer and the server. To do this, I can use the <strong>new-TimeSpan<\/strong> cmdlet to measure the difference between the two <strong>Get-Date<\/strong> commands. Keep in mind that part of the time offset could be attributable to the time it takes to make the remote connection and to run the command and to return the information. How this affects the time, however, depends on whether my local system time is fast or slow when compared with the remote server. The command itself is rather simple. I use the <strong>Get-Date<\/strong> cmdlet from my local computer as the start of the timespan. I then use the time from the remote server as the end time. The command and associated results are shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; new-timespan -Start (get-date) -end (icm dc1 {get-date})<\/p>\n<p style=\"padding-left: 30px\">Days&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Hours&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Minutes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Seconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Milliseconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : -311<\/p>\n<p style=\"padding-left: 30px\">Ticks&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : -3116406<\/p>\n<p style=\"padding-left: 30px\">TotalDays&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : -3.60695138888889E-06<\/p>\n<p style=\"padding-left: 30px\">TotalHours&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : -8.65668333333333E-05<\/p>\n<p style=\"padding-left: 30px\">TotalMinutes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : -0.00519401<\/p>\n<p style=\"padding-left: 30px\">TotalSeconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : -0.3116406<\/p>\n<p style=\"padding-left: 30px\">TotalMilliseconds : -311.6406<\/p>\n<h2>Sync with an external time source<\/h2>\n<p>The time skew between my computer and the server is less than half of a second&mdash;certainly well within tolerance. But what if I had a greater time skew? Well, I would first resync my server with the external time source. To do this, I would use the <strong>W32tm<\/strong> command.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong>&nbsp; &nbsp;For more information about the topic of resyncing time sources, see <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/cc784553(WS.10).aspx\" target=\"_blank\">this article<\/a> at the TechNet Library.\nThe following command runs <strong>W32tm<\/strong> and tells the remote server DC1 to resync with Time.Windows.Com. The output states that the results are successful.<\/p>\n<p style=\"padding-left: 30px\">&nbsp;PS C:&gt; icm dc1 {w32tm \/config \/syncfromflags:manual \/manualpeerlist:time.windows.com}\nThe command completed successfully.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong> &nbsp;&nbsp;By using the command to resync with the time source requires Admin rights.\nOnce I have caused the server to resync with the external time source, I will resync my local computer with the server. This command is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; w32tm \/resync<\/p>\n<p style=\"padding-left: 30px\">Sending resync command to local computer\nThe command completed successfully.\nOnce I have resynced both the time server and the workstation, I use the <strong>New-TimeSpan<\/strong> command to monitor for any difference.<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; new-timespan -Start (get-date) -end (icm dc1 {get-date})<\/p>\n<p style=\"padding-left: 30px\">Days&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Hours&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Minutes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Seconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Milliseconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : -270<\/p>\n<p style=\"padding-left: 30px\">Ticks&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : -2707415<\/p>\n<p style=\"padding-left: 30px\">TotalDays&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : -3.13358217592593E-06<\/p>\n<p style=\"padding-left: 30px\">TotalHours&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : -7.52059722222222E-05<\/p>\n<p style=\"padding-left: 30px\">TotalMinutes&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;: -0.00451235833333333<\/p>\n<p style=\"padding-left: 30px\">TotalSeconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : -0.2707415<\/p>\n<p style=\"padding-left: 30px\">TotalMilliseconds : -270.7415\nIf I need to double-check the time source settings on the remote time server, I use the <strong>W32tm<\/strong> command to query the configuration. The command is shown here.<\/p>\n<p style=\"padding-left: 30px\">icm dc1 {w32tm \/query \/configuration}\n&nbsp;Join me tomorrow when I will talk about more cool Windows PowerShell stuff.\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n<strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to easily compare the time between two computers. Microsoft Scripting Guy, Ed Wilson, is here. In two days, I will be speaking to the AZPOSH Windows PowerShell user group in Phoenix, Arizona. No, I am not flying out there&mdash;I have enough travel coming [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[86,31,3,130,45],"class_list":["post-4720","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-basic-information","tag-operating-system","tag-scripting-guy","tag-servers","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to easily compare the time between two computers. Microsoft Scripting Guy, Ed Wilson, is here. In two days, I will be speaking to the AZPOSH Windows PowerShell user group in Phoenix, Arizona. No, I am not flying out there&mdash;I have enough travel coming [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4720","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=4720"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4720\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=4720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}