Powershell Format Date/Time for Display and for FileName

The following is code I copy and paste a lot into my various Powershell prorams.

The commented out line creates a backup zip file, so I like to have the file name with the date/time in it.

$fmtDateTime = $(get-date -f "MM/dd/yyyy HH:mm:ss")
Write-Host "Start Time: $fmtDateTime"

$fileDateTime = $(get-date -f "MM_dd_yyyy__HH_mm_ss")
Write-Host "File DateTime: $fileDateTime"

$filename = "C_Temp_$fileDateTime" 

#.zip suffix is automatically added 
#Compress-Archive -Path C:\Temp -DestinationPath f:\Backup\TempDir\$filename


Leave a Reply