I have functions here and there, that will at times, create files to store generated information. Part of this process is naming the file. For me, I’ll often add the date and time to my file’s name, in order to know it’s creation time, at a glance.
So what does this mean? It means that I’ll often have to jump over to my ConsoleHost, or an old function, I suppose, to be certain I’m using the same date time format. Well, that may end today, right after this post is published. Now, I’ll have another place to look — my own website — to ensure my consistency when it comes to including the same naming.
You can’t use the standard output of Get-Date in a file name, and you probably wouldn’t want to anyway. This is due to the colons included in the time; they’re invalid characters for a file name.
PS > Get-Date Friday, October 28, 2016 9:21:28 PM
But when we put the Get-Date output in a file name, it changes the output. We loose the day of the week and month as words, and instead get a date with forward slashes, such as 10/28/2016. This conversion happens when we put the cmdlet inside a string. Take a look.
PS > New-Item -Path "$(Get-Date).txt" -ItemType File New-Item : Cannot find drive. A drive with the name '10/28/2016 09' does not exist...
PS > "$(Get-Date).txt" 10/28/2016 09:27:04.txt
The error makes sense though, as it’s parsing 10/28/2016 as a path. Not really the point here, but good to know. Either way, those slashes and colons aren’t going in a file name. It’s not permitted.
I’ve had a consistent file naming convention that includes the date and time for awhile now. What I really like about my date format is that files are automatically sorted by year, then month, then day, and then the time.
PS > Get-Date -Format 'DyyyyMMddThhmmsstt' D20161028T093059PM PS > New-Item -Path "Get-Date -Format 'DyyyyMMddThhmmsstt'" -ItemType File Directory: C:\Users\tommymaynard Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 10/28/2016 9:32 PM 0 Get-Date -Format 'DyyyyMMddThhmmsstt'
Oops, notice the problem in that second example above? I need to ensure the command inside my string is being treated as a command, and not just standard text. In the previous example, it used my actual command, as the file name. Let’s try that again.
PS > New-Item -Path "$(Get-Date -Format 'DyyyyMMddThhmmsstt').txt" -ItemType File Directory: C:\Users\tommymaynard Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 10/28/2016 9:34 PM 0 D20161028T093445PM.txt
In this final example, we included some text around the date, so that I can better distinguish the reason for the file, but still have the date and time included in the name. We also used the subexpression operator — $() — to make sure my command was treated as such. Oh, did you notice the capital D and T? I used these as separators to help better display the (D)ate and (T)ime. It makes the files name easier for me to visually parse.
PS > New-Item -Path "User_Disable_Log($(Get-Date -Format 'DyyyyMMddThhmmsstt')).txt" -ItemType File Directory: C:\Users\tommymaynard Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 10/28/2016 9:37 PM 0 User_Disable_Log(D20161028T095316PM).txt
That’s all for now! Enjoy the weekend.