Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
share|improve this question

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
    
Using an expandable string with a subexpression is more complicated than needed. –  Jay Bazuzi Dec 7 '12 at 16:42
    
While (Get-Date).ToString(...) works, Get-Date -Format ... is more idiomatic PowerShell. –  Jay Bazuzi Dec 7 '12 at 16:42

Try this:

mkdir (Get-Date -Format "yyyy-MM-dd")
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.