PowerShell Logo Small

Get-Date



This is the built-in help made by Microsoft for the command 'Get-Date', in PowerShell version 2 - as retrieved from Windows version 'Microsoft® Windows Vista™ Ultimate ' PowerShell help files on 2016-06-23.

For PowerShell version 3 and up, where you have Update-Help, this command was run just before creating the web pages from the help files.

SYNOPSIS

Gets the current date and time.

SYNTAX


Get-Date [-Format <string>] [[-Date] <DateTime>] [-Day <int>] [-DisplayHint {Date | Time | DateTime}] [-Hour <int>] [-Minute <int>] [-Month <int>
] [-Second <int>] [-Year <int>] [<CommonParameters>]
Get-Date [-UFormat <string>] [[-Date] <DateTime>] [-Day <int>] [-DisplayHint {Date | Time | DateTime}] [-Hour <int>] [-Minute <int>] [-Month <int
>] [-Second <int>] [-Year <int>] [<CommonParameters>]



Search powershellhelp.space

DESCRIPTION


The Get-Date cmdlet gets a DateTime object that represents the current date or a date that you specify. It can format the date and time in severa
l Windows 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 progra
ms.



<

RELATED LINKS

Online version: http://go.microsoft.com/fwlink/?LinkID=113313
Set-Date
New-TimeSpan

REMARKS

<

Examples


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

C:\PS>get-date -DisplayHint date

Tuesday, June 13, 2006



Description
-----------
This command retrieves a DateTime object, but it displays only the date. It uses the DisplayHint parameter to indicate that only the date is to b
e displayed.








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

C:\PS>get-date -format g

6/13/2006 12:43 PM



Description
-----------
This command retrieves the current date and time and formats it in short-date and short-time format. It uses the .NET Framework "g" format specif
ier (General [short date and short time]) to specify the format.








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

C:\PS>get-date -uformat "%Y / %m / %d / %A / %Z"

2006 / 06 / 13 / Tuesday / -07



Description
-----------
This command retrieves the current date and time and formats it as specified by the command. In this case, the format includes the full year (%Y)
, the two-digit numeric month (%m), the date (%d), the full day of the week (%A), and the offset from UTC ("Zulu").








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

C:\PS>(get-date -year 2000 -month 12 -day 31).dayofyear

366



Description
-----------
This command displays the day of the year for the current date. For example, December 31 is the 365th day of 2006, but it is the 366th day of 200
0.








-------------------------- EXAMPLE 5 --------------------------

C:\PS>$a = get-date

C:\PS> $a.IsDaylightSavingTime()

True



Description
-----------
These commands tell you whether the current date and time are adjusted for daylight savings time in the current locale.

The first command creates a variable named $a and then assigns the object retrieved by Get-Date to the $a variable. Then, it uses the IsDaylightS
avingTime method on the object in $a.

To see the properties and methods of the DateTime object, type:
"get-date | get-member".








-------------------------- EXAMPLE 6 --------------------------

C:\PS>$a = get-date

C:\PS> $a.ToUniversalTime()

Tuesday, June 13, 2006 8:09:19 PM



Description
-----------
These commands convert the current date and time to UTC time.

The first command creates a variable named $a and then assigns the object retrieved by Get-Date to the $a variable. Then, it uses the ToUniversal
Time method on the object in $a.








-------------------------- EXAMPLE 7 --------------------------

C:\PS>$a = get-wmiobject win32_bios -computer server01

$a | format-list -property Name, @{Label="BIOS Age"; `
Expression={(get-date) - $_.ConvertToDateTime($_.ReleaseDate)}}

Name : Default System BIOS
BIOS Age : 1345.17:31:07.1091047



Description
-----------
Windows Management Instrumentation (WMI) uses a different date-time object than the .NET Framework date-time object that Get-Date returns. To use
date-time information from WMI in a command with date-time information from Get-Date, you have to use the ConvertToDateTime method to convert WM
I CIM_DATETIME objects to .NET Framework DateTime objects.

The commands in this example display the name and age of the BIOS on a remote computer, Server01.

The first command uses the Get-WmiObject cmdlet to get an instance of the Win32_BIOS class on Server01 and then stores it in the $a variable.

The second command uses the pipeline operator (|) to send the WMI object stored in $a to the Format-List cmdlet. The Property parameter of Format
-List is used to specify two properties to display in the list, "Name" and "BIOS Age". The "BIOS Age" property is specified in a hash table. The
table includes the Label key, which specifies the name of the property, and the Expression key, which contains the expression that calculates the
BIOS age. The expression uses the ConvertToDateTime method to convert each instance of ReleaseDate to a .NET Framework DateTime object. Then, th
e value is subtracted from the value of the Get-Date cmdlet, which, without parameters, gets the current date.

The backtick character (`) is the line continuation character in Windows PowerShell.








-------------------------- EXAMPLE 8 --------------------------

C:\PS>get-date

Tuesday, June 13, 2006 12:43:42 PM



Description
-----------
This command gets a DateTime object and displays the current date and time in the long date and long time formats for the system locale, as thoug
h you typed "get-date -format F".








-------------------------- EXAMPLE 9 --------------------------

C:\PS>get-date

C:\PS> Tuesday, September 26, 2006 11:25:31 AM

c:\PS>(get-date).ToString()
9/26/2006 11:25:31 AM

C:\PS>get-date | add-content test.txt
# Adds 9/26/2006 11:25:31 AM

C:\PS>get-date -format F | add-content test.txt
# Adds Tuesday, September 26, 2006 11:25:31 AM



Description
-----------
These commands demonstrate how to use Get-Date with Add-Content and other cmdlets that convert the DateTime object that Get-Date generates to a s
tring.

The first command shows that the default display from a "get-date" command is in long-date and long-time format.

The second command shows that the default display from the ToString() method of the DateTime object is in short-date and short-time format.

The third command uses a pipeline operator to send the DateTime object to the Add-Content cmdlet, which adds the content to the Test.txt file. Be
cause Add-Content uses the ToString() method of the DateTime object, the date that is added is in short-date and short-time format.

The fourth command uses the Format parameter of Get-Date to specify the format. When you use the Format or UFormat parameters, Get-Date generates
a string, not a DateTime object. Then, when you send the string to Add-Content, it adds the string to the Test.txt file without changing it.