Skip to content

Adding the date to a filename with Powershell

22/06/2016

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.

Advertisements

From → Powershell

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: