none
Setting System Time with PowerShell

    Question

  • How can we modify or change system time (not date) using windows powershell? I was trying to change system time by below command:-

    set-date -adjust "Monday, October 17, 2011 5:35:25 PM"

    But it throws error saying Cannot convert value "Monday, October 17, 2011 5:35:25 PM" to type System.TimeSpan.

    I googled a lot but can't find a way to set the SystemTime using powerShell to a given time. I am baically trying to develop a script that accepts time in hours, minutes, seconds ( like "5:35:25 PM") and then sets the system time to "5:35:25 PM" some thing like below.

    function Set-Time( [DateTime]$Time)
    {
    set-date -adjust $Time
    }
    
    Set-Time  "Monday, October 17, 2011 5:35:25 PM"


    • Edited by vilas6_it Tuesday, October 18, 2011 12:08 AM
    Tuesday, October 18, 2011 12:07 AM

Answers

  • You cant pass "Monday, October 17" to set-date as it only handles numbered dates

     

    set-date "17/11/2011 5:35:25 PM"
    


    this will work however.

    have a read of the following websites to get an idea of how set-date and get-date work

    http://technet.microsoft.com/en-us/library/ff730960.aspx

    http://technet.microsoft.com/en-au/library/ee176960.aspx

     

    • Marked as answer by vilas6_it Tuesday, October 18, 2011 3:40 PM
    Tuesday, October 18, 2011 1:09 AM
  • This will also work:

     

    function Set-Time ([string]$dateTime) {
        $newDate = Get-Date $dateTime
        Set-Date $newDate 
        }
    
    Set-Time 'Monday, October 17, 2011 5:35:25 PM'
    

     


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    • Edited by Bigteddy Tuesday, October 18, 2011 4:52 AM
    • Marked as answer by vilas6_it Tuesday, October 18, 2011 3:39 PM
    Tuesday, October 18, 2011 4:52 AM

All replies

  • You cant pass "Monday, October 17" to set-date as it only handles numbered dates

     

    set-date "17/11/2011 5:35:25 PM"
    


    this will work however.

    have a read of the following websites to get an idea of how set-date and get-date work

    http://technet.microsoft.com/en-us/library/ff730960.aspx

    http://technet.microsoft.com/en-au/library/ee176960.aspx

     

    • Marked as answer by vilas6_it Tuesday, October 18, 2011 3:40 PM
    Tuesday, October 18, 2011 1:09 AM
  • This will also work:

     

    function Set-Time ([string]$dateTime) {
        $newDate = Get-Date $dateTime
        Set-Date $newDate 
        }
    
    Set-Time 'Monday, October 17, 2011 5:35:25 PM'
    

     


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    • Edited by Bigteddy Tuesday, October 18, 2011 4:52 AM
    • Marked as answer by vilas6_it Tuesday, October 18, 2011 3:39 PM
    Tuesday, October 18, 2011 4:52 AM
  • Note that you can adjust the time only (leaving the date alone), by calling the function like this:

    Set-Time '5:35:25 PM'
    


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Tuesday, October 18, 2011 3:54 PM
  • You can also use WMI to set the time on remote computers.

    (From the Windows PowerShell 2.0 Bible)

    $Credential = Get-Credential
    foreach ($Server in Get-Content -Path c:\Scripts\Servers.txt)
    {
        $TimeObject = @{
        Class = "Win32_OperatingSystem"
        ComputerName = $Server
        Credential = $Credential
        EnableAllPrivileges = $true
        }
        $CurrentTime = ([wmi]'').ConvertFromDateTime($(Get-Date))
        (Get-WmiObject @TimeObject).SetDateTime($CurrentTime)
    }

    Karl


    My Blog: http://unlockpowershell.wordpress.com
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    Tuesday, October 18, 2011 8:32 PM
  • as I am using server 2012 powershell

    I use Command as Set-Date -date "5/1/2014 2:44 PM" in Powershell

    Click to access Data and TIme of Powershell

    Thank You


    sagarpdalvi

    Sunday, January 05, 2014 9:14 AM
  • Worked like a charm

    Friday, March 28, 2014 3:10 AM