Do you want to know how to format a date while working with PowerShell Get-Date? Here, I will show you how to use PowerShell Get-Date
format to retrieve the current date and time, and format it in various ways. In this tutorial, we’ll explore how to use PowerShell Get-Date
to format dates in different patterns, such as yyyymmdd
, yyyymmddhhmmss
, mm/dd/yyyy
, and dd/mm/yyyy
.
PowerShell Get-Date Format yyyymmdd
The yyyymmdd
format is a common date representation that stands for a four-digit year, two-digit month, and two-digit day, all concatenated together without any separators.
In PowerShell, you can obtain the current date in yyyymmdd
format using the Get-Date
cmdlet with the -Format
parameter:
Get-Date -Format "yyyyMMdd"
Example:
# This will output the current date as something like 20240104
$currentDate = Get-Date -Format "yyyyMMdd"
Write-Output $currentDate
Here, you can see the output in the screenshot below after I executed the above PowerShell script.
PowerShell Get-Date Format yyyymmddhhmmss
If you need more than just the date and require the time as well, the yyyymmddhhmmss
format comes into play. This pattern includes the year, month, day, hour, minute, and second, providing a complete timestamp without separators.
To format the current date and time in yyyymmddhhmmss
format, use PowerShell Get-Date
with the -Format
parameter:
Get-Date -Format "yyyyMMddHHmmss"
Example:
# This will output the current date and time as something like 20240104153045
$currentDateTime = Get-Date -Format "yyyyMMddHHmmss"
Write-Output $currentDateTime
Here is the output like in the below screenshot.
PowerShell Format Date dd/mm/yyyy
The dd/mm/yyyy
format is a widely used date representation around the world, where the day is followed by the month and then the year. To achieve this format in PowerShell, you again use the Get-Date
cmdlet with the -Format
parameter:
Get-Date -Format "dd/MM/yyyy"
Example:
# This will output the current date as something like 04/01/2024
$currentDateFormatted = Get-Date -Format "dd/MM/yyyy"
Write-Output $currentDateFormatted
To ensure that you get the dd/MM/yyyy
format regardless of the regional settings, you can use the below PowerShell script.
$currentDateUSFormat = Get-Date -UFormat "%d/%M/%Y"
Write-Output $currentDateUSFormat
You can see the output like the below screenshot.
PowerShell Format Date mm/dd/yyyy
The mm/dd/yyyy
format is another common date representation, particularly in the United States, where the month precedes the day and year. To format a date in this style in PowerShell, you can use the Get-Date
cmdlet with the -Format
parameter, similar to the other formats we’ve discussed.
Here’s how you can get the current date in the mm/dd/yyyy
format:
Get-Date -Format "MM/dd/yyyy"
Example:
# This will output the current date as something like 01/04/2024
$currentDateUSFormat = Get-Date -Format "MM/dd/yyyy"
Write-Output $currentDateUSFormat
In this example, the MM
represents the two-digit month, dd
represents the two-digit day, and yyyy
represents the four-digit year. The slashes /
are used as separators to distinguish each part of the date clearly.
To ensure that you get the mm/dd/yyyy
format regardless of the regional settings, you can use the below PowerShell script.
$currentDateUSFormat = Get-Date -UFormat "%m/%d/%Y"
Write-Output $currentDateUSFormat
The -UFormat
parameter uses Unix format specifiers and should give you the date in the desired format of mm/dd/yyyy
. For example, if today’s date is January 4, 2024, the output should be 01/04/2024
.
Check out the screenshot below for the output:
Conclusion
Formatting dates in PowerShell is straightforward with the Get-Date
cmdlet. By using the -Format
parameter with the desired pattern in PowerShell Get-Date, you can easily transform the current date and time into a string that fits your needs.
Here I have explained to you the below PowerShell get-date format examples.
- PowerShell get-date format yyyymmdd
- PowerShell get-date format yyyymmddhhmmss
- PowerShell format date dd/mm/yyyy
- PowerShell Format Date mm/dd/yyyy
You may also like:
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com