Date of file name from variable

Hi all! A newbie question, perhaps, but I didn’t found a solution about that. The code is:

$date = Get-Date -DisplayHint Date
New-Item -ItemType file “D:$($date).txt”

And ISE says, that’s given path format is unsupported. Why so and how make it work?


I am also a newbie. You will need to convert the date to a string to create the file:

$date = (Get-Date -DisplayHint Date).ToString(“D”)
New-Item -ItemType File “D:$date.txt”

It’s because filenames do not support the “:” or “/” characters.

$date = Get-Date -DisplayHint Date
New-Item -ItemType file "F:\Temp\$($date).txt"


F:\09/28/2015 08:51:22.txt
New-Item : The given path's format is not supported.
At line:3 char:1
+ New-Item -ItemType file "F:\Temp\$($date).txt"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [New-Item], NotSupportedException
    + FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.NewItemCommand

Use this:

$date = Get-Date -Format "D"
New-Item -ItemType file "F:\Temp\$($date).txt"

Thanks, Juan! It’s what I looked for.