Get-Date

Get the current date and time.

Syntax
      Get-Date [[-date] DateTime]
         [-displayHint {Date | Time | DateTime}]
           {[-format string] | [-uFormat string]}
              [-year int] [-month int] [-day int] [-hour int]
                 [-minute int] [-second int] [CommonParameters]

key
   -date DateTime
       By default, Get-Date returns the current system date and time.
       The -date parameter allows you to specify
       (usually via the pipeline) a specific date and time. 

   -displayHint DisplayHintType
       Display only the Date, only the Time or the DateTime.
		This does not affect the DateTime object that is retrieved.

   -format string
       Display the date and time in the .NET format
       as indicated by String representing a format specifier. 
        
   -uFormat string
       Display the date and time in Unix format.

-year -month -day -hour -minute -second These allow you to set individual items to be displayed in place of the current date/time. e.g. you could set the time to 12:00 CommonParameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable, -OutBuffer -OutVariable.

When you use -format or -uformat, PowerShell will retrieve only the properties that it needs to display the date in the format that you specify. As a result, some properties and methods of DateTime objects might not be available.

Date Properties:

$a = (get-date).day
$a = (get-date).dayofweek
$a = (get-date).dayofyear
$a = (get-date).hour
$a = (get-date).millisecond
$a = (get-date).minute
$a = (get-date).month
$a = (get-date).second
$a = (get-date).timeofday
$a = (get-date).year

To see all the properties and methods of the DateTime object, type get-date get-member

If you specify a value that is greater than the number of days in the month, PowerShell adds the number of days to the month and displays the result. For example, "get-date -month 2 -day 31" displays "March 3", not "February 31".

Examples

Retrieve the current date and time, but display only the date:

PS C:\> get-date -DisplayHint date

Retrieve the current date and time:

PS C:\> $now=Get-Date -format "dd-MMM-yyyy HH:mm"

Retrieve the current date and time, display as a General short date/time:

PS C:\> get-date -format g

Display the day of the year:

PS C:\> (get-date).dayofyear

Get the day of the week as an integer (1=Monday, 7=Sunday):

PS C:\> [Int]$dow = Get-Date | Select-Object -ExpandProperty DayOfWeek
PS C:\> $dow

Display yesterdays date, using the .AddDays method:

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

Display daylight savings and UTC:

PS C:\>$a = get-date
$a.IsDaylightSavingTime()
$a.ToUniversalTime()

Display the bios date of a remote machine using WMI:

PS C:\>$a = get-wmiobject win32_bios -computer SERVER64
$a | format-list -property Name, @{Label="BIOS Date "; `
Expression={$_.ConvertToDateTime($_.ReleaseDate)}}

The backtick character (`) is the line continuation character

“Carpe Diem - Seize the day” ~ Horace

Related:

Set-Date - Set system time on the host system
New-Timespan - Create a timespan object
Equivalent bash command: date - Display or change the date


© Copyright SS64.com 1999-2014
Some rights reserved