3

I'm trying to create a powershell script that creates a new folder with the current date (formatted as yyyy-MM-dd) as a name.

Here's what I have so far:

PS C:\Users\me\Desktop> powershell.exe -command "new-item ($(get-location)
+ (Get-Date).year + "-" + (Get-Date).month + "-" + (Get-Date).day) -type directo
ry"
Die Benennung "C:\Users\me\Desktop" wurde nicht als Name eines Cmdlet, ein
er Funktion, einer Skriptdatei oder eines ausführbaren Programms erkannt. Überp
rüfen Sie die Schreibweise des Namens, oder ob der Pfad korrekt ist (sofern ent
halten), und wiederholen Sie den Vorgang.
Bei Zeile:1 Zeichen:35
+ new-item (C:\Users\me\Desktop <<<<  + (Get-Date).year +  - + (Get-Date).
month + - + (Get-Date).day) -type directory
+ CategoryInfo          : ObjectNotFound: (C:\Users\j.moore\Desktop:String
) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

2 Answers 2

4

Try this:

 New-Item "$((get-date).toString('yyyy-MM-dd'))" -ItemType directory

or:

md "$((get-date).toString('yyyy-MM-dd'))"

Explanation from this site:

$() enables you to use the output of a command in a string

The () around get-date again say "Execute this first" so that we can use the Methods of the DateTime Object returned by get-date

the toString() Method of a DateTime object takes a formattingstring as input.

2
  • Using an expandable string with a subexpression is more complicated than needed.
    – Jay Bazuzi
    Dec 7, 2012 at 16:42
  • While (Get-Date).ToString(...) works, Get-Date -Format ... is more idiomatic PowerShell.
    – Jay Bazuzi
    Dec 7, 2012 at 16:42
0

Try this:

mkdir (Get-Date -Format "yyyy-MM-dd")

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .