Compartir a través de


Set-Date

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

Sintaxis

Date (Es el valor predeterminado).

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

Adjust

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

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.

You must have administrative rights to change the system date and time. On Windows, start PowerShell with the Run as administrator option.

Ejemplos

Example 1: Add three days to the system date

This command adds three days to the current system date. It doesn't affect the time. The command uses the Date parameter to specify the date.

The Get-Date cmdlet returns the current date as a DateTime object. The DateTime object's AddDays method adds a specified number of days (3) to the current DateTime object.

Set-Date -Date (Get-Date).AddDays(3)

Example 2: Set the system clock back 10 minutes

This example sets the current system time back by 10 minutes.

The Adjust parameter allows you to specify an interval of change (minus ten minutes) in the standard time format for the locale.

The DisplayHint parameter tells PowerShell to display only the time, but it doesn't affect the DateTime object that Set-Date returns.

Set-Date -Adjust -0:10:0 -DisplayHint Time

Example 3: Set the date and time to a variable value

These commands change the system date and time on local 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.

$T = Get-Date
Set-Date -Date $T

Example 4: Add 90 minutes to the system clock

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 saves it 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.

$90mins = New-TimeSpan -Minutes 90
Set-Date -Adjust $90mins

5: Change to a specific date and time

The following example sets the date and time to a specific value.

PS> Get-Date

Monday, June 10, 2024 2:05:48 PM

PS> Set-Date '6/11/2024 2:05:48 PM'

Tuesday, June 11, 2024 2:05:48 PM

Parámetros

-Adjust

Specifies the value for which this cmdlet adds or subtracts 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.

Propiedades del parámetro

Tipo:TimeSpan
Valor predeterminado:None
Admite caracteres comodín:False
DontShow:False

Conjuntos de parámetros

Adjust
Posición:0
Mandatory:True
Valor de la canalización:False
Valor de la canalización por nombre de propiedad:True
Valor de los argumentos restantes:False

-Confirm

Prompts you for confirmation before running the cmdlet.

Propiedades del parámetro

Tipo:SwitchParameter
Valor predeterminado:False
Admite caracteres comodín:False
DontShow:False
Alias:cf

Conjuntos de parámetros

(All)
Posición:Named
Mandatory:False
Valor de la canalización:False
Valor de la canalización por nombre de propiedad:False
Valor de los argumentos restantes:False

-Date

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 doesn't change the date.

Propiedades del parámetro

Tipo:DateTime
Valor predeterminado:None
Admite caracteres comodín:False
DontShow:False

Conjuntos de parámetros

Date
Posición:0
Mandatory:True
Valor de la canalización:True
Valor de la canalización por nombre de propiedad:True
Valor de los argumentos restantes:False

-DisplayHint

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 doesn't affect the DateTime object that Get-Date retrieves.

Propiedades del parámetro

Tipo:DisplayHintType
Valor predeterminado:None
Valores aceptados:Date, Time, DateTime
Admite caracteres comodín:False
DontShow:False

Conjuntos de parámetros

(All)
Posición:Named
Mandatory:False
Valor de la canalización:False
Valor de la canalización por nombre de propiedad:False
Valor de los argumentos restantes:False

-WhatIf

Shows what would happen if the cmdlet runs. The cmdlet isn't run.

Propiedades del parámetro

Tipo:SwitchParameter
Valor predeterminado:False
Admite caracteres comodín:False
DontShow:False
Alias:wi

Conjuntos de parámetros

(All)
Posición:Named
Mandatory:False
Valor de la canalización:False
Valor de la canalización por nombre de propiedad:False
Valor de los argumentos restantes:False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

Entradas

DateTime

You can pipe a date to this cmdlet.

Salidas

DateTime

This cmdlet returns an object that represents the date that it set.

Notas

  • 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 Methods and TimeSpan Methods in the .NET SDK.