在powershell中创建当前日期作为名称的文件夹

San*_*hos 11 directory powershell date

我正在写一个powershell脚本.我想知道如何创建一个当前日期为名称的文件夹.文件夹名称应为"yyyy-MM-dd"(根据.NET自定义格式字符串).

我知道要创建文件夹我需要使用此命令:

New-Item -ItemType directory -Path "some path"
Run Code Online (Sandbox Code Playgroud)

可能的解决方案是(如果我想在与脚本相同的目录中创建文件夹:

$date = Get-Date
$date = $date.ToString("yyyy-MM-dd")
New-Item -ItemType directory -Path ".\$date"
Run Code Online (Sandbox Code Playgroud)

有没有办法链接命令,所以我不需要创建变量?

not*_*tme 22

是.

New-Item -ItemType Directory -Path ".\$((Get-Date).ToShortDateString())"
Run Code Online (Sandbox Code Playgroud)

或者作为alroc建议,以便获得相同的格式,无论文化.

New-Item -ItemType Directory -Path ".\$((Get-Date).ToString('yyyy-MM-dd'))"
Run Code Online (Sandbox Code Playgroud)


alr*_*roc 12

不要用ToShortDateString()@notjustme写的; 其格式取决于区域和语言控制面板中的区域设置和日期格式设置.例如,在我的电脑上,这将产生以下目录名称:

C:\Users\me\09\18\2014
Run Code Online (Sandbox Code Playgroud)

改为明确设置日期字符串的格式.

New-Item -ItemType Directory -Path ".\$((Get-Date).ToString('yyyy-MM-dd'))"
Run Code Online (Sandbox Code Playgroud)