PowerShell Commands

Set-Date

Set-Date [-Adjust*] <TimeSpan> [-Confirm] [-DisplayHint {Date | Time | DateTime}] [-WhatIf] [<CommonParameters>]
Set-Date [-Date*] <DateTime> [-Confirm] [-DisplayHint {Date | Time | DateTime}] [-WhatIf] [<CommonParameters>]

The Set-Date cmdlet changes the system date and time on the computer to a date and time that you specify. You can specify a new date and/or time by typing a string or by passing a DateTime or TimeSpan object to Set-Date . To specify a new date or time, use the Date parameter. To specify a change interval, use the Adjust parameter.

Parameters

-Adjust <TimeSpan>

  • This value is required
  • Default value is None
  • Accepts pipeline input ByPropertyName

Specifies the value for which this cmdlet adds or subtracts from the current date and time. can type an adjustment in standard date and time format for your locale or use the Adjust parameter to pass a TimeSpan object from New-TimeSpan to Set-Date .

-Confirm [<SwitchParameter>]

  • Default value is False
  • Accepts pipeline input False

Prompts you for confirmation before running the cmdlet.

-Date <DateTime>

  • This value is required
  • Default value is None
  • Accepts pipeline input ByPropertyName

Changes the date and time to the specified values. You can type a new date in the short date format and a time in the standard time format for your locale. Or, you can pass a DateTime object from Get-Date.

If you specify a date, but not a time, Set-Date changes the time to midnight on the specified date. If you specify only a time, it does not change the date.

-DisplayHint <DisplayHintType>

  • Default value is None
  • Accepts pipeline input False

Specifies which elements of the date and time are displayed.The acceptable values for this parameter are:

- Date. displays only the date. - Time. displays only the time. - DateTime. displays the date and time.

This parameter affects only the display. It does not affect the DateTime object that Get-Date retrieves.

-WhatIf [<SwitchParameter>]

  • Default value is False
  • Accepts pipeline input False

Shows what would happen if the cmdlet runs. The cmdlet is not run.

<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,ErrorAction, ErrorVariable, WarningAction, WarningVariable,OutBuffer, PipelineVariable, and OutVariable.

Inputs
System.DateTime
You can pipe a date to Set-Date .
Outputs
System.DateTime
Set-Date returns an object that represents the date that it set.
Examples
  1. Add three days to the system date:
    PS C:\> Set-Date -Date (Get-Date).AddDays(3)
    

    This command adds three days to the current system date. It does not affect the time. The command uses the Date parameter to specify the date. It uses the Get-Date cmdlet to get the current date and time and applies the AddDays .NET method for DateTime objects with a value of 3 (days).

  2. Set the system clock back 10 minutes:
    PS C:\> Set-Date -Adjust -0:10:0 -DisplayHint Time
    

    This command sets the current system time back by 10 minutes. It uses the Adjust parameter to specify an interval of change and the time change (minus ten minutes) in standard time format for the locale. The DisplayHint parameter tells Windows PowerShell to display only the time, but it does not affect the DateTime object that Set-Date returns.

  3. Set the date and time to a variable value:
    PS C:\> $T = Get-Date
    PS C:\> Set-Date -Date $T
    

    These commands change the system date and time on the computer to the date and time saved in the variable $T. The first command gets the date and stores it in $T. The second command uses the Date parameter to pass the DateTime object in $T to the Set-Date cmdlet.

  4. Add 90 minutes to the system clock:
    PS C:\> $90mins = New-TimeSpan -Minutes 90
    PS C:\> Set-Date -Adjust $90mins
    

    These commands advance the system time on the local computer by 90 minutes. The first command uses the New-TimeSpan cmdlet to create a TimeSpan object with a 90-minute interval, and then it saves the TimeSpan object in the $90mins variable. The second command uses the Adjust parameter of Set-Date to adjust the date by the value of the TimeSpan object in the $90mins variable.

Additional Notes
 Use this cmdlet cautiously when changing the date and time on the computer. The change might prevent the 
 computer from receiving system-wide events and updates that are triggered by a date or time. Use the WhatIf 
 and Confirm* parameters to avoid errors. You can use standard .NET methods with the DateTime and TimeSpan 
 objects used with Set-Date , such as AddDays , AddMonths , and FromFileTime *. For more information, see 
 DateTime Methodshttps://msdn.microsoft.com/en-us/library/system.datetime_methods(v=vs.110).aspx and TimeSpan 
 Methodshttps://msdn.microsoft.com/en-us/library/system.timespan_methods(v=vs.110).aspx.

This work is licensed under a Creative Commons Attribution 4.0 International. It is attributed to Microsoft Corporation and can be found here.

PowerShell Commands