Author Posts

September 28, 2015 at 5:00 am

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?

September 28, 2015 at 5:56 am

Dmitry,

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"

September 28, 2015 at 5:57 am

It's because filenames do not support the ":" or "/" characters.

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

Results:

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"
"F:\$($date).txt"
New-Item -ItemType file "F:\Temp\$($date).txt"

September 28, 2015 at 10:03 pm

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