I often need to create file names that include the date & time the file was created in the name. Iâ€™ve come up with all sorts of ways to do but this I think is the simplest.
I want the date in this format: year-month-day-hour-minute-second. In other words a format that is easily sortable. I discovered that if you convert a data to a string there is a formatter that does most of the work for you. Thatâ€™s a lower case s.
You canâ€™t have a : symbol in a file name so need to get rid of those
To complete the file name
PS> $datestring = (Get-Date).ToString(“s”).Replace(“:”,”-”)
PS> $file = “c:\folder\Prefix_$datestring.txt”
Iâ€™ve done this as a two step process otherwise when you replace the : you also take out the one for the disk drive â€“ oops