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:

$day = (get-date).day
$dayofweek = (get-date).dayofweek
$dayofyear = (get-date).dayofyear
$hour = (get-date).hour
$ms = (get-date).millisecond
$minute = (get-date).minute
$month = (get-date).month
$second = (get-date).second
$time = (get-date).timeofday
$year = (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 will display "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 and store in the variable $start:

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

Get the current time with Hours, Minutes and Seconds:

PS C:\> $time_now = Get-Date -format "HH:mm:ss"
14:43:04

Retrieve the current date and time in strict ISO 8601 format:

PS C:\> Get-Date -format s
2018-11-26T14:43:04

Get the current date and time with fractions of a second, in a format suitable to be used as a Windows filename:

PS C:\> get-date -format yyyy-MM-ddTHH-mm-ss-ff
2018-11-26T14-45-02-33

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 (0=Sunday, 6=Saturday):

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)

Get a specific date:

PS C:\> $mydate = Get-Date -date "2018-02-28"

Display daylight savings and UTC:

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

# or display in ISO 8601 format:
$a.ToUniversalTime().ToString('yyyy-MM-dd HH:mm:ss')

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 PowerShell Cmdlets:

Set-Date - Set system time on the host system.
New-Timespan - Create a timespan object.
Rename-Item - Rename items to include the date or time.
Equivalent bash command: date - Display or change the date.


 
Copyright © SS64.com 1999-2019
Some rights reserved