Summary: Use the –format option in Get-Date to change the output.
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?
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’
I guess it must be lower-case ‘mm’ to get the minutes: MM_dd_yyyy-HH_mm_ss
Should be Get-Date –format ‘MM_dd_yyyy-HH_mm_ss’ to display minutes (with leading zero) instead of months…
Also, one can use the ToString method of the datime object:
(Get-Date).ToString(‘MM_dd_yyyy-HH_MM_ss’)
This way, you can perform calculations before formating the string:
(Get-Date).AddMonth(1).ToString(‘MM_dd_yyyy-HH_MM_ss’)
Edit
This way, you can perform calculations before formating the string:
(Get-Date).AddMonths(1).ToString(‘MM_dd_yyyy-HH_MM_ss’)
Is the week of the year fixed yet? (get-date -uformat %V)
I know it was bugged in previous versions
You would want to use lower case ‘mm’ for minutes.
Capital ‘MM’ will give you the month.
Actually its AddMonths
Here are the others
AddDays
AddHours
AddMilliseconds
AddMinutes
AddMonths
AddSeconds
AddTicks
AddYears
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’
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’