How to format date string in PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

By default the when you run the (Get-Date) cmdlet, its output is in the below format.

PS C:\WINDOWS\system32> Get-Date
18 March 2020 22:56:18

You can format the above output in the various format supported by PowerShell.

Examples

  • d – Short date pattern.

PS C:\WINDOWS\system32> Get-Date -Format d
18-03-2020
  • D – Long date pattern

PS C:\WINDOWS\system32> Get-Date -Format D
18 March 2020
  • f – Full date pattern with a short time pattern.

PS C:\WINDOWS\system32> Get-Date -Format f
18 March 2020 23:01
  • F – Full date pattern with a long time pattern.

PS C:\WINDOWS\system32> Get-Date -Format F
18 March 2020 23:02:22
  • g – General date pattern with a short time pattern.

PS C:\WINDOWS\system32> Get-Date -Format g 18-03-2020 23:03
  • G – General date pattern with a long time pattern.

PS C:\WINDOWS\system32> Get-Date -Format G
18-03-2020 23:05:39
  • M/m – Month day pattern.

PS C:\WINDOWS\system32> Get-Date -Format M
18 March
PS C:\WINDOWS\system32> Get-Date -Format m
18 March
  • O,o – Round trip date/time pattern

PS C:\WINDOWS\system32> Get-Date -Format O
2020-03-18T23:08:36.8098960+05:30
PS C:\WINDOWS\system32> Get-Date -Format o
2020-03-18T23:08:36.8098960+05:30
  • R,r – RFC1123 pattern

PS C:\WINDOWS\system32> Get-Date -Format R
Wed, 18 Mar 2020 23:10:06 GMT
PS C:\WINDOWS\system32> Get-Date -Format r
Wed, 18 Mar 2020 23:10:06 GMT
  • s – Sortable Date/Time pattern

PS C:\WINDOWS\system32> Get-Date -Format s
2020-03-18T23:12:12
  • t – Short time pattern.

PS C:\WINDOWS\system32> Get-Date -Format t
23:13
  • T – Long time pattern.

PS C:\WINDOWS\system32> Get-Date -Format T
23:13:09
  • u – Universal Sortable date/time pattern.

PS C:\WINDOWS\system32> Get-Date -Format u
2020-03-19 20:21:37Z
  • U – Universal Full date/time format.

PS C:\WINDOWS\system32> Get-Date -Format U
19 March 2020 14:51:41
  • Y,y – Year month pattern.

PS C:\WINDOWS\system32> Get-Date -Format y
March, 2020
PS C:\WINDOWS\system32> Get-Date -Format Y
March, 2020

You can also format Get-Date into the .NET format and for that, you need to use –Format parameter.

.NET format specifier can be used as below.

ddddDay of the week – Full Name
MMMonth number
ddDay of the month – 2 digits
yyyyYear in 4-digit format
HH:mmTime in 24-hour format –no seconds
KTimezone format in UTC (Universal Time Coordinate)

You can use any of the above combinations to format the date in the required format.

Example

Get-Date -Format "dd/MM/yyyy"
19-03-2020


PS C:\WINDOWS\system32> Get-Date -Format "dd/MM/yyyy -- dddd"
19-03-2020 -- Thursday


PS C:\WINDOWS\system32> Get-Date -Format "HH:mm K"
20:44 +05:30

You can format the date string into the universal format as well and for that, you need to use –Uformat parameter. Below specifiers used for the universal format.

%ADay of the week – Full Name
%mMonth number
%dDay of the month – 2 digits
%YYear in 4-digit format
%RTime in 24-hour format –no seconds
%ZTimezone offset from UTC

Example

Get-Date -UFormat "%d %m %Y"
19 03 2020


Get-Date -UFormat %R
21:15
raja
Published on 20-Mar-2020 07:04:28
Advertisements