Export (0) Print
Expand All

Set-Date

Published: February 29, 2012

Updated: August 15, 2012

Applies To: Windows PowerShell 2.0, Windows PowerShell 3.0

Set-Date

Changes the system time on the computer to a time that you specify.

Syntax

Parameter Set: Date
Set-Date [-Date] <DateTime> [-DisplayHint <DisplayHintType> ] [-Confirm] [-WhatIf] [ <CommonParameters>]

Parameter Set: Adjust
Set-Date [-Adjust] <TimeSpan> [-DisplayHint <DisplayHintType> ] [-Confirm] [-WhatIf] [ <CommonParameters>]




Detailed Description

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>

Adds or subtracts the specified value from the current date and time. You 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.


Aliases

none

Required?

true

Position?

1

Default Value

none

Accept Pipeline Input?

True (ByPropertyName)

Accept Wildcard Characters?

false

-Date<DateTime>

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 Date-Time 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.


Aliases

none

Required?

true

Position?

1

Default Value

none

Accept Pipeline Input?

True (ByValue, ByPropertyName)

Accept Wildcard Characters?

false

-DisplayHint<DisplayHintType>

Determines which elements of the date and time are displayed.

Valid values 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.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Confirm

Prompts you for confirmation before running the cmdlet.


Required?

false

Position?

named

Default Value

false

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-WhatIf

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


Required?

false

Position?

named

Default Value

false

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

<CommonParameters>

This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutBuffer, and -OutVariable. For more information, see  about_CommonParameters (http://go.microsoft.com/fwlink/p/?LinkID=113216).

Inputs

The input type is the type of the objects that you can pipe to the cmdlet.

Outputs

The output type is the type of the objects that the cmdlet emits.

  • System.DateTime

    Set-Date returns an object that represents the date that it set.


Notes

  • Use this cmdlet cautiously. 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 Methods" and "TimeSpan Methods."

Examples

-------------------------- EXAMPLE 1 --------------------------

Description

-----------

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).


C:\PS>Set-Date -Date (Get-Date).AddDays(3)

-------------------------- EXAMPLE 2 --------------------------

Description

-----------

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.


C:\PS>set-date -adjust -0:10:0 -displayHint time

-------------------------- EXAMPLE 3 --------------------------

Description

-----------

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.


C:\PS>$t = get-date
PS C:\>set-date -date $t

-------------------------- EXAMPLE 4 --------------------------

Description

-----------

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.


C:\PS>$90mins = new-timespan -minutes 90
PS C:\>set-date -adjust $90mins

Related topics



Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft