
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to format date string in PowerShell?
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.
dddd | Day of the week β Full Name |
MM | Month number |
dd | Day of the month β 2 digits |
yyyy | Year in 4-digit format |
HH:mm | Time in 24-hour format βno seconds |
K | Timezone 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.
%A | Day of the week β Full Name |
%m | Month number |
%d | Day of the month β 2 digits |
%Y | Year in 4-digit format |
%R | Time in 24-hour format βno seconds |
%Z | Timezone offset from UTC |
Example
Get-Date -UFormat "%d %m %Y" 19 03 2020
Get-Date -UFormat %R 21:15