How to Format Date in PowerShell [With Examples]

The Get-Date cmdlet in PowerShell is used to retrieve the current date and time. Everyone knows, but I will explain how to format data in PowerShell with examples.

To format the current date and time in PowerShell as yyyymmddhhmmss, you can use the Get-Date cmdlet with the -Format parameter. The command is Get-Date -Format "yyyyMMddHHmmss", which will output the date and time in a compact, sortable format.

Get-date Format in PowerShell

The Get-Date cmdlet in PowerShell is used to get the current date and time of the system. It can be customized to display the date and time in various formats.

Syntax

The basic syntax for PowerShell Get-Date is:

Get-Date

However, to format the date and time, you can use the -Format parameter:

Get-Date -Format "formatString"

PowerShell get-date Formatting Options with Examples

Now, let me show you different formatting options with examples in PowerShell.

1. PowerShell Get-Date Format yyyymmddhhmmss

To get the date and time in the format yyyymmddhhmmss, you can use the following PowerShell command:

Get-Date -Format "yyyyMMddHHmmss"

Example:

PS C:\> Get-Date -Format "yyyyMMddHHmmss"

This command returns the current date and time in a compact format, which is useful for filenames or log entries. Formatting the date and time in this way ensures that the information is concise and sortable.

Here is the output you can see in the screenshot below:

PowerShell Get-Date Format yyyymmddhhmmss

2. PowerShell Get-Date Format yyyymmdd

To get just the date in the format yyyymmdd, use:

Get-Date -Format "yyyyMMdd"

Example:

PS C:\> Get-Date -Format "yyyyMMdd"

This format is ideal for creating date-stamped files or directories. It provides a clear and unambiguous way to represent dates, making organizing and retrieving files based on their creation date easier.

Here is the output in the screenshot below:

PowerShell Get-Date Format yyyymmdd

Check out Get Date Without Time in PowerShell

3. PowerShell Get-Date Format yyyymmddhhmmss with Milliseconds

If you need the date, time, and milliseconds, you can use:

Get-Date -Format "yyyyMMddHHmmssfff"

Example:

PS C:\> Get-Date -Format "yyyyMMddHHmmssfff"

This format includes milliseconds, which is useful for precise timestamping. This level of precision can be critical in scenarios where events occur in rapid succession and need to be accurately logged.

4. PowerShell Format Date dd/MM/yyyy

To format the date as dd/MM/yyyy, use:

Get-Date -Format "dd/MM/yyyy"

Example:

PS C:\> Get-Date -Format "dd/MM/yyyy"
15/09/2024

This format is commonly used in many countries and can be useful for international applications. By using this format, you can ensure that the date is understood correctly in regions where the day precedes the month in date notation.

5. PowerShell Get Current Date Format yyyy-MM-dd

To get the current date in the format yyyy-MM-dd, use:

Get-Date -Format "yyyy-MM-dd"

Example:

PS C:\> Get-Date -Format "yyyy-MM-dd"
2024-09-15

This ISO format is widely used in databases and data exchange formats. It provides a standardized way to represent dates, making it ideal for use in systems that need to exchange date information reliably.

You can check the output in the screenshot below:

PowerShell Get Current Date Format yyyy-MM-dd

Check out Check If Date Is Older Than 30 Days in PowerShell

6. PowerShell Current Date Format yyyy-MM-dd HH:mm:ss

To get the current date and time in the format yyyy-MM-dd HH:mm:ss, use:

Get-Date -Format "yyyy-MM-dd HH:mm:ss"

Example:

PS C:\> Get-Date -Format "yyyy-MM-dd HH:mm:ss"
2024-09-15 12:30:45

This format is useful for logging and timestamping events. It combines the date and time in a readable format, making it easy to track when events occurred.

7. PowerShell Date Format yyyy-MM-dd HH:mm:ss

Similar to the previous example, but explicitly showing how to include both date and time:

Get-Date -Format "yyyy-MM-dd HH:mm:ss"

Example:

PS C:\> Get-Date -Format "yyyy-MM-dd HH:mm:ss"
2024-09-15 12:07:16

This format ensures consistency in date and time representation. By using a consistent format, you can avoid confusion and ensure that dates and times are interpreted correctly.

Here is the output you can see in the screenshot below:

PowerShell Date Format yyyy-MM-dd HH:mm:ss

Read Get Date Differences Between Two Dates in PowerShell

PowerShell format date mm/dd/yyyy

In PowerShell, you can format dates using the Get-Date cmdlet and the -Format parameter. To format a date as MM/dd/yyyy, you can use the ToString() method or the -Format parameter directly.

Example Using Get-Date with -Format Parameter

Here is a simple example of how to format the current date to MM/dd/yyyy in PowerShell:

# Get the current date and format it as MM/dd/yyyy
$currentDate = Get-Date -Format "MM/dd/yyyy"
Write-Output $currentDate

This will output the current date in the format MM/dd/yyyy.

Example Using ToString() Method

Alternatively, you can use the ToString() method to format the current date in MM/dd/yyyy format:

# Get the current date and format it as MM/dd/yyyy using ToString() method
$currentDate = (Get-Date).ToString("MM/dd/yyyy")
Write-Output $currentDate

Both methods will produce the same output.

Example with Custom Date

If you want to format a specific date, you can create a DateTime object and then format it. Here is the complete script.

# Create a DateTime object for a specific date
$customDate = [datetime]::ParseExact("2024-09-17", "yyyy-MM-dd", $null)

# Format the custom date as MM/dd/yyyy
$formattedDate = $customDate.ToString("MM/dd/yyyy")
Write-Output $formattedDate

This will output 09/17/2024 for the given custom date.

Conclusion

In this tutorial, I have explained how to format date in PowerShell. I have also shown different PowerShell get-date formatting options with examples.

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.