PowerTip: Get the time and date as a string of numbers by using PowerShell

Summary: Use the –format option in Get-Date to change the output.

Hey, Scripting Guy! Question I’d like to build some log files and have the date and time as part of the name. Is there a way to show the date and time in a format where it’s all numbers?

Hey, Scripting Guy! Answer Absolutely! Just use the –format option with Get-Date and supply a format for the output. To see the format as month, day, year, hour, minutes, and seconds, use this example:

Get-Date –format ‘MM_dd_yyyy-HH_MM_ss’

The Doctor

Comments (9)

  1. GeeBee says:

    I guess it must be lower-case ‘mm’ to get the minutes: MM_dd_yyyy-HH_mm_ss

  2. Marco G says:

    Should be Get-Date –format ‘MM_dd_yyyy-HH_mm_ss’ to display minutes (with leading zero) instead of months…

  3. hpotsirhc says:

    Also, one can use the ToString method of the datime object:

    This way, you can perform calculations before formating the string:

    1. Edit
      This way, you can perform calculations before formating the string:

  4. AdamBast says:

    Is the week of the year fixed yet? (get-date -uformat %V)
    I know it was bugged in previous versions

  5. w.lewis says:

    You would want to use lower case ‘mm’ for minutes.

    Capital ‘MM’ will give you the month.

  6. Bob Williams says:

    Actually its AddMonths

    Here are the others

  7. todd says:

    Correction, the second set of M’s should be lowercase to reflect minutes instead of the month. Your command runs without error, but the returned time will likely be incorrect. Should be:

    Get-Date –format ‘MM_dd_yyyy-HH_mm_ss’

  8. James Brown says:

    The example given uses capital M’s for both the month and the minute, which is incorrect. Capital M’s are for the month, and lower-case m’s are for minutes:

    Get-Date –format ‘MM_dd_yyyy-HH_mm_ss’

Skip to main content