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