Format

Using the -Format parameter in the PowerShell command, you can display date and time in a specific format that you specify. The -Format parameter accepts all values which are allowed in the Microsoft .Net framework and included in the DateTimeFormatInfo Class. There are different format options available that you can use to format your date and time results according to your requirement.

Note :

that the result returned by -Format parameter is not the DateTime object; instead, they will a String result. We are mentioned some of the standard format specifiers information and their short description, which are provided by Microsoft.

Standard Strings Format Specifiers

Some of the standard .NET strings format specifiers that are mostly used are defined below:

SpecifierDescription
ddddreturns a day of the week in full name
MMreturns month number
ddreturns a day of the month in 2 digits
yyyyreturns a year in 4-digit
HH:mmreturns a time in 24-hour format without seconds
Kreturns the time zone offset from Universal Time Coordinate (UTC)

For example:

Let see an example to retrieve the date and time in a .NET format specifier using the following – Format parameter and several .NET format specifiers:

  1. Get-Date -Format “dddd MM/dd/yyyy HH:mm k”  
PowerShell Get-Date Format

Optional Format list

Date
dreturns short date pattern
Dreturns long date pattern
freturns full date/time pattern (short time)
Freturns full date/time pattern (long time)
greturns general date/time (short time)
Greturns general date/time (long time)
m or Mreturns month day pattern
oreturns round-trip date/time pattern
r or Rreturns RFC1123 pattern
sreturns sortable date/time pattern; conforms to ISO 8601
u or Ureturns sortable date/time pattern
y or Yreturns year month pattern
Time
treturns short time pattern
Treturns long time pattern

Let’s apply be following format one by one and see their output:

  1. Get-Date -Format d  
  2. Get-Date -Format D  
  3. Get-Date -Format f  
  4. Get-Date -Format F  
  5. Get-Date -Format g  
  6. Get-Date -Format G  
  7. Get-Date -Format m  
  8. Get-Date -Format y  
PowerShell Get-Date Format

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *