Adding the date to a filename with Powershell

Following on from my previous post, Write to a text file using Powershell, I found it useful to be able to include the date into the log filename.

$LogFile ="C:\PsTools\Method01-$(get-date -f yyyy-MM-dd).log"
$DateTime = Get-Date
 Write-Output $DateTime "Performing some operation" | Add-Content $LogFile

You can see from this script that the date format will give you, as example, 2016-06-22 as the date part of the filename. You can experiment with different formats to get something that you are happy with although this is currently my preference.






Leave a Reply

Your email address will not be published. Required fields are marked *