Set-Date (Microsoft.PowerShell.Utility) - PowerShell (2024)

  • Reference
Module:
Microsoft.PowerShell.Utility

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

Syntax

Set-Date [-Date] <DateTime> [-DisplayHint <DisplayHintType>] [-WhatIf] [-Confirm] [<CommonParameters>]
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 youspecify.

You can specify a new date and/or time by typing a string or by passing a DateTime orTimeSpan object to Set-Date. To specify a new date or time, use the Date parameter.To specify a change interval, use the Adjust parameter.

Examples

Example 1: Add three days to the system date

This command adds three days to the current system date. It does not affect the time. The commanduses the Date parameter to specify the date.

The Get-Date cmdlet returns the current date as a DateTime object. The DateTime object'sAddDays 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 thestandard time format for the locale.

The DisplayHint parameter tells PowerShell to display only the time, but it does notaffect 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 thevariable $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 theSet-Date cmdlet.

$T = Get-DateSet-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-minuteinterval, and saves it in the $90mins variable.

The second command uses the Adjust parameter of Set-Date to adjust the date by the value ofthe TimeSpan object in the $90mins variable.

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

Parameters

-Adjust

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 Adjustparameter to pass a TimeSpan object from New-TimeSpan to Set-Date.

Type:TimeSpan
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-Confirm

Prompts you for confirmation before running the cmdlet.

Type:SwitchParameter
Aliases:cf
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters: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 yourlocale. 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 specifieddate. If you specify only a time, it does not change the date.

Type:DateTime
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-DisplayHint

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

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

Type:DisplayHintType
Accepted values:Date, Time, DateTime
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-WhatIf

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

Type:SwitchParameter
Aliases:wi
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

Inputs

DateTime

You can pipe a date to this cmdlet.

Outputs

DateTime

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

Notes

  • Use this cmdlet cautiously when changing the date and time on the computer. The change mightprevent the computer from receiving system-wide events and updates that are triggered by a date ortime. Use the WhatIf and Confirm parameters to avoid errors.
  • You can use standard .NET methods with the DateTime and TimeSpan objects used withSet-Date, such as AddDays, AddMonths, and FromFileTime. For more information, seeDateTime Methods andTimeSpan Methods in the .NET SDK.
  • Get-Date
  • New-TimeSpan
Set-Date (Microsoft.PowerShell.Utility) - PowerShell (2024)

FAQs

How do you set the date in PowerShell? ›

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.

How do I get a specific date in PowerShell? ›

The Get-Date cmdlet gets a DateTime object that represents the current date or a date that you specify. Get-Date can format the date and time in several .NET and UNIX formats. You can use Get-Date to generate a date or time character string, and then send the string to other cmdlets or programs.

How do I get the full date in PowerShell? ›

PowerShell provides several options for converting Get-Date to a string. One of the commonly used methods is the ToString() method, which allows you to specify the desired format. This will convert the current date and time to a string format in the format yyyy-MM-dd HH:mm:ss .

How to set date in shell script? ›

The Bash date command, combined with a format string, is your key to manipulating and displaying dates. For instance, date '+%Y-%m-%d' will output the current date in the 'YYYY-MM-DD' format. In this example, we've used the date command with a format string '+%Y-%m-%d' .

How to set a variable in PowerShell? ›

To create a new variable, use an assignment statement to assign a value to the variable. You don't have to declare the variable before using it. The default value of all variables is $null . To get a list of all the variables in your PowerShell session, type Get-Variable .

How to convert string to datetime in PowerShell? ›

In many cases, it may be required to convert a string to a date variable or a date-time object. In PowerShell, this can be done in many ways. Some of the ways in which this can be done by using type casting, getting the date in US format, using parsing of Date Time.

How to run a PowerShell script? ›

In File Explorer (or Windows Explorer), right-click the script filename and then select Run with PowerShell. The Run with PowerShell feature starts a Windows PowerShell session that has an execution policy of Bypass, runs the script, and closes the session.

How to get the last modified date of a folder in PowerShell? ›

How to get last modified file in directory using PowerShell
  1. Identify the domain from which you want to retrieve the report.
  2. Find the LDAP attributes you need to fetch the report.
  3. Identify the primary DC to retrieve the report.
  4. Compile the script.
  5. Execute it in Windows PowerShell.

How to add timestamp in PowerShell script? ›

How to do it...
  1. Open PowerShell ISE. Go to Start | Accessories | Windows PowerShell | Windows PowerShell.
  2. Add the following script and run it: $timestamp = Get-Date -Format "yyyy-MMM-dd-hhmmtt" #display timestamp $timestamp.

How to check last update date in PowerShell? ›

Press the Windows Key + X and select Windows PowerShell (Admin). Type in wmic qfe list. You will see a list of updates including the HotFix (KB) number and link, description, comments, installed date, and more.

How do I change the date output format? ›

Choosing from the Date Format List
  1. Select the cells you want to format.
  2. Click Ctrl+1 or Command+1.
  3. Select the “Numbers” tab.
  4. From the categories, choose “Date”
  5. From the “Type” menu, select the date format you want.
Nov 20, 2023

How to start services from PowerShell? ›

Examples
  1. Start a service by using its name: PS C:\> Start-Service -Name "eventlog" ...
  2. Display information without starting a service: PS C:\> Start-Service -DisplayName *remote* -WhatIf.

How to get timestamp in cmd? ›

To display timestamp on command prompt, use the special character \D{} for PS1 shell variable. It is possible to display arbitrary time format by putting a special character based on strftime() function into {} . For more information on special characters, refer to the online manual “STRFTIME(3) man 3 strftime. ”

How to set date in cmd? ›

2. Type "date" into the command prompt window and press "Enter." The current date setting will now display. To change it, type the proper date into the window in the "mm-dd-yy" format -- for example, "05-30-13" for May 30, 2013 -- and press "Enter." The new date setting will now be saved.

How to display date with TimeZone in PowerShell? ›

In PowerShell, use [System. TimeZone] and invoke the ConvertTimeBySystemZoneID static method, which returns the date-time value of the given time zone! In PowerShell, use [System. TimeZone] and invoke the ConvertTimeBySystemZoneID static method, which returns the date-time value of the given time zone!

Top Articles
Latest Posts
Article information

Author: Prof. An Powlowski

Last Updated:

Views: 5944

Rating: 4.3 / 5 (64 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Prof. An Powlowski

Birthday: 1992-09-29

Address: Apt. 994 8891 Orval Hill, Brittnyburgh, AZ 41023-0398

Phone: +26417467956738

Job: District Marketing Strategist

Hobby: Embroidery, Bodybuilding, Motor sports, Amateur radio, Wood carving, Whittling, Air sports

Introduction: My name is Prof. An Powlowski, I am a charming, helpful, attractive, good, graceful, thoughtful, vast person who loves writing and wants to share my knowledge and understanding with you.