PowerShell: How to Use Get-Date with Milliseconds


You can use the Get-Date cmdlet in PowerShell to get the current date and time.

By default, the time component will be shown with hours, minutes and seconds.

In order to display milliseconds, you can use the ToString() method with the following syntax:

(Get-Date).ToString("yyyy-MM-dd HH:mm:ss.fff")

Here is what the various commands do:

  • yyyy: 4-digit year number
  • MM: 2-digit month number
  • dd: 2-digit day number
  • HH: 2-digit hours in 24-hour format
  • mm: 2-digit minutes
  • ss: 2-digit seconds
  • fff: 3-digit milliseconds

The following example shows how to use this syntax in practice.

Example: Use Get-Date and Display Milliseconds in PowerShell

Suppose that we use the Get-Date cmdlet to retrieve the current date and time:

Get-Date

The following screenshot shows how to use this syntax in practice:

This returns the following date and time:

  • Monday, April 22, 2024 8:11:54 AM

Notice that milliseconds are not included in the time.

To force the milliseconds to be shown, we must use the following syntax:

(Get-Date).ToString("yyyy-MM-dd HH:mm:ss.fff") 

The following screenshot shows how to use this syntax in practice:

PowerShell Get-Date with milliseconds

This returns the following date and time:

  • 2024-04-22 08:12:17.810

Notice that milliseconds are included in the time up to three decimal places.

Note that you can include more f characters after the decimal to display the milliseconds with even greater precision.

For example, we could use the following syntax to display the milliseconds up to six decimal places:

(Get-Date).ToString("yyyy-MM-dd HH:mm:ss.ffffff") 

The following screenshot shows how to use this syntax in practice:

PowerShell Get-Date with milliseconds up to 6 decimal places

This returns the following date and time:

  • 2024-04-22 08:12:36.955301

Notice that milliseconds are included in the time up to six decimal places.

Feel free to specify as many decimal places as you would like.

Note: You can find the complete documentation for the Get-Date cmdlet in PowerShell here.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

How to Use Get-Date and Display Timezone in PowerShell
How to Compare Dates in PowerShell
How to Format a DateTime in PowerShell
How to Calculate Date Difference in PowerShell

Leave a Reply